]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_tree.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tree.h
CommitLineData
42526146
PE
1// RB tree implementation -*- C++ -*-
2
83ffe9cd 3// Copyright (C) 2001-2023 Free Software Foundation, Inc.
42526146
PE
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
42526146 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
42526146 24
725dc051
BK
25/*
26 *
27 * Copyright (c) 1996,1997
28 * Silicon Graphics Computer Systems, Inc.
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1994
40 * Hewlett-Packard Company
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Hewlett-Packard Company makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 *
50 *
51 */
52
f910786b 53/** @file bits/stl_tree.h
729e3d3f 54 * This is an internal header file, included by other library headers.
29f69649 55 * Do not attempt to use it directly. @headername{map,set}
725dc051
BK
56 */
57
046d30f4
PC
58#ifndef _STL_TREE_H
59#define _STL_TREE_H 1
725dc051 60
3fa591d4
JW
61#pragma GCC system_header
62
725dc051 63#include <bits/stl_algobase.h>
1ff9402d 64#include <bits/allocator.h>
725dc051 65#include <bits/stl_function.h>
8bd22a3c 66#include <bits/cpp_type_traits.h>
ff90a89e 67#include <ext/alloc_traits.h>
1f069142 68#if __cplusplus >= 201103L
2dbe56bd
JW
69# include <ext/aligned_buffer.h>
70#endif
71#if __cplusplus > 201402L
72# include <bits/node_handle.h>
1f069142 73#endif
725dc051 74
12ffa228
BK
75namespace std _GLIBCXX_VISIBILITY(default)
76{
77_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 78
0bd9bdb4 79#if __cplusplus > 201103L
b8806796 80# define __cpp_lib_generic_associative_lookup 201304L
0bd9bdb4
JW
81#endif
82
8bd22a3c
BK
83 // Red-black tree class, designed for use in implementing STL
84 // associative containers (set, multiset, map, and multimap). The
85 // insertion and deletion algorithms are based on those in Cormen,
86 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
87 // 1990), except that
88 //
89 // (1) the header cell is maintained with links not only to the root
90 // but also to the leftmost node of the tree, to enable constant
91 // time begin(), and to the rightmost node of the tree, to enable
92 // linear time performance when used with the generic set algorithms
93 // (set_union, etc.)
fee84d55 94 //
8bd22a3c
BK
95 // (2) when a node being deleted has two children its successor node
96 // is relinked into its place, rather than copied, so that the only
97 // iterators invalidated are those referring to the deleted node.
98
655d7821 99 enum _Rb_tree_color { _S_red = false, _S_black = true };
725dc051 100
d3d526ac
BK
101 struct _Rb_tree_node_base
102 {
103 typedef _Rb_tree_node_base* _Base_ptr;
b4c70e89 104 typedef const _Rb_tree_node_base* _Const_Base_ptr;
ed6814f7
BI
105
106 _Rb_tree_color _M_color;
107 _Base_ptr _M_parent;
108 _Base_ptr _M_left;
109 _Base_ptr _M_right;
110
111 static _Base_ptr
0e1a966a 112 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
d3d526ac
BK
113 {
114 while (__x->_M_left != 0) __x = __x->_M_left;
115 return __x;
116 }
725dc051 117
b4c70e89 118 static _Const_Base_ptr
0e1a966a 119 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89
GB
120 {
121 while (__x->_M_left != 0) __x = __x->_M_left;
122 return __x;
123 }
124
ed6814f7 125 static _Base_ptr
0e1a966a 126 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
d3d526ac
BK
127 {
128 while (__x->_M_right != 0) __x = __x->_M_right;
129 return __x;
130 }
b4c70e89
GB
131
132 static _Const_Base_ptr
0e1a966a 133 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89
GB
134 {
135 while (__x->_M_right != 0) __x = __x->_M_right;
136 return __x;
137 }
d3d526ac 138 };
725dc051 139
a4dec0d6
FD
140 // Helper type offering value initialization guarantee on the compare functor.
141 template<typename _Key_compare>
142 struct _Rb_tree_key_compare
143 {
144 _Key_compare _M_key_compare;
145
146 _Rb_tree_key_compare()
147 _GLIBCXX_NOEXCEPT_IF(
148 is_nothrow_default_constructible<_Key_compare>::value)
149 : _M_key_compare()
150 { }
151
152 _Rb_tree_key_compare(const _Key_compare& __comp)
153 : _M_key_compare(__comp)
154 { }
155
156#if __cplusplus >= 201103L
157 // Copy constructor added for consistency with C++98 mode.
158 _Rb_tree_key_compare(const _Rb_tree_key_compare&) = default;
159
160 _Rb_tree_key_compare(_Rb_tree_key_compare&& __x)
161 noexcept(is_nothrow_copy_constructible<_Key_compare>::value)
162 : _M_key_compare(__x._M_key_compare)
163 { }
164#endif
165 };
166
167 // Helper type to manage default initialization of node count and header.
168 struct _Rb_tree_header
169 {
170 _Rb_tree_node_base _M_header;
171 size_t _M_node_count; // Keeps track of size of tree.
172
173 _Rb_tree_header() _GLIBCXX_NOEXCEPT
174 {
175 _M_header._M_color = _S_red;
176 _M_reset();
177 }
178
179#if __cplusplus >= 201103L
180 _Rb_tree_header(_Rb_tree_header&& __x) noexcept
181 {
182 if (__x._M_header._M_parent != nullptr)
183 _M_move_data(__x);
184 else
185 {
186 _M_header._M_color = _S_red;
187 _M_reset();
188 }
189 }
190#endif
191
192 void
193 _M_move_data(_Rb_tree_header& __from)
194 {
4f28d159 195 _M_header._M_color = __from._M_header._M_color;
a4dec0d6
FD
196 _M_header._M_parent = __from._M_header._M_parent;
197 _M_header._M_left = __from._M_header._M_left;
198 _M_header._M_right = __from._M_header._M_right;
199 _M_header._M_parent->_M_parent = &_M_header;
200 _M_node_count = __from._M_node_count;
201
202 __from._M_reset();
203 }
204
205 void
206 _M_reset()
207 {
208 _M_header._M_parent = 0;
209 _M_header._M_left = &_M_header;
210 _M_header._M_right = &_M_header;
211 _M_node_count = 0;
212 }
213 };
214
d3d526ac
BK
215 template<typename _Val>
216 struct _Rb_tree_node : public _Rb_tree_node_base
217 {
218 typedef _Rb_tree_node<_Val>* _Link_type;
ff90a89e
JW
219
220#if __cplusplus < 201103L
d3d526ac 221 _Val _M_value_field;
25bbe9bc 222
ff90a89e
JW
223 _Val*
224 _M_valptr()
225 { return std::__addressof(_M_value_field); }
226
227 const _Val*
228 _M_valptr() const
229 { return std::__addressof(_M_value_field); }
230#else
2097b5b0 231 __gnu_cxx::__aligned_membuf<_Val> _M_storage;
ff90a89e
JW
232
233 _Val*
234 _M_valptr()
235 { return _M_storage._M_ptr(); }
236
237 const _Val*
238 _M_valptr() const
239 { return _M_storage._M_ptr(); }
25bbe9bc 240#endif
d3d526ac 241 };
ed6814f7 242
b8add594 243 _GLIBCXX_PURE _Rb_tree_node_base*
1cf1c842 244 _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
d3d526ac 245
b8add594 246 _GLIBCXX_PURE const _Rb_tree_node_base*
1cf1c842 247 _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
e135a038 248
b8add594 249 _GLIBCXX_PURE _Rb_tree_node_base*
1cf1c842 250 _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
d3d526ac 251
b8add594 252 _GLIBCXX_PURE const _Rb_tree_node_base*
1cf1c842 253 _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
e135a038
BK
254
255 template<typename _Tp>
b4c70e89 256 struct _Rb_tree_iterator
d3d526ac 257 {
e135a038
BK
258 typedef _Tp value_type;
259 typedef _Tp& reference;
260 typedef _Tp* pointer;
261
b4c70e89 262 typedef bidirectional_iterator_tag iterator_category;
e0b9bc23 263 typedef ptrdiff_t difference_type;
e135a038 264
e0b9bc23
FD
265 typedef _Rb_tree_iterator<_Tp> _Self;
266 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
267 typedef _Rb_tree_node<_Tp>* _Link_type;
ed6814f7 268
0e1a966a 269 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
a7a44441 270 : _M_node() { }
b4c70e89 271
e182017e 272 explicit
2097b5b0 273 _Rb_tree_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
8bd22a3c 274 : _M_node(__x) { }
b4c70e89 275
ed6814f7 276 reference
0e1a966a 277 operator*() const _GLIBCXX_NOEXCEPT
ff90a89e 278 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
d3d526ac 279
ed6814f7 280 pointer
0e1a966a 281 operator->() const _GLIBCXX_NOEXCEPT
ff90a89e 282 { return static_cast<_Link_type> (_M_node)->_M_valptr(); }
d3d526ac 283
ed6814f7 284 _Self&
0e1a966a 285 operator++() _GLIBCXX_NOEXCEPT
ed6814f7 286 {
b4c70e89 287 _M_node = _Rb_tree_increment(_M_node);
ed6814f7 288 return *this;
d3d526ac 289 }
725dc051 290
ed6814f7 291 _Self
0e1a966a 292 operator++(int) _GLIBCXX_NOEXCEPT
d3d526ac
BK
293 {
294 _Self __tmp = *this;
b4c70e89 295 _M_node = _Rb_tree_increment(_M_node);
d3d526ac
BK
296 return __tmp;
297 }
ed6814f7
BI
298
299 _Self&
0e1a966a 300 operator--() _GLIBCXX_NOEXCEPT
b4c70e89
GB
301 {
302 _M_node = _Rb_tree_decrement(_M_node);
303 return *this;
304 }
d3d526ac 305
ed6814f7 306 _Self
0e1a966a 307 operator--(int) _GLIBCXX_NOEXCEPT
d3d526ac
BK
308 {
309 _Self __tmp = *this;
b4c70e89 310 _M_node = _Rb_tree_decrement(_M_node);
d3d526ac
BK
311 return __tmp;
312 }
b4c70e89 313
e0b9bc23
FD
314 friend bool
315 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
316 { return __x._M_node == __y._M_node; }
e135a038 317
93843da6 318#if ! __cpp_lib_three_way_comparison
e0b9bc23
FD
319 friend bool
320 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
321 { return __x._M_node != __y._M_node; }
93843da6 322#endif
e135a038 323
b4c70e89 324 _Base_ptr _M_node;
c8a1cf1a 325 };
d3d526ac 326
e135a038
BK
327 template<typename _Tp>
328 struct _Rb_tree_const_iterator
329 {
fee84d55 330 typedef _Tp value_type;
e135a038
BK
331 typedef const _Tp& reference;
332 typedef const _Tp* pointer;
d3d526ac 333
e135a038 334 typedef _Rb_tree_iterator<_Tp> iterator;
d3d526ac 335
e135a038 336 typedef bidirectional_iterator_tag iterator_category;
fee84d55 337 typedef ptrdiff_t difference_type;
d3d526ac 338
fee84d55
FD
339 typedef _Rb_tree_const_iterator<_Tp> _Self;
340 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
341 typedef const _Rb_tree_node<_Tp>* _Link_type;
ed6814f7 342
0e1a966a 343 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
a7a44441 344 : _M_node() { }
e135a038 345
e182017e 346 explicit
2097b5b0 347 _Rb_tree_const_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
8bd22a3c 348 : _M_node(__x) { }
e135a038 349
0e1a966a 350 _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPT
8bd22a3c 351 : _M_node(__it._M_node) { }
e135a038 352
6b6d5d09 353 iterator
0e1a966a 354 _M_const_cast() const _GLIBCXX_NOEXCEPT
2097b5b0 355 { return iterator(const_cast<typename iterator::_Base_ptr>(_M_node)); }
6b6d5d09 356
e135a038 357 reference
0e1a966a 358 operator*() const _GLIBCXX_NOEXCEPT
ff90a89e 359 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
e135a038
BK
360
361 pointer
0e1a966a 362 operator->() const _GLIBCXX_NOEXCEPT
ff90a89e 363 { return static_cast<_Link_type>(_M_node)->_M_valptr(); }
e135a038 364
ed6814f7 365 _Self&
0e1a966a 366 operator++() _GLIBCXX_NOEXCEPT
ed6814f7 367 {
e135a038 368 _M_node = _Rb_tree_increment(_M_node);
ed6814f7 369 return *this;
e135a038
BK
370 }
371
ed6814f7 372 _Self
0e1a966a 373 operator++(int) _GLIBCXX_NOEXCEPT
e135a038
BK
374 {
375 _Self __tmp = *this;
376 _M_node = _Rb_tree_increment(_M_node);
377 return __tmp;
378 }
ed6814f7
BI
379
380 _Self&
0e1a966a 381 operator--() _GLIBCXX_NOEXCEPT
e135a038
BK
382 {
383 _M_node = _Rb_tree_decrement(_M_node);
384 return *this;
385 }
386
ed6814f7 387 _Self
0e1a966a 388 operator--(int) _GLIBCXX_NOEXCEPT
e135a038
BK
389 {
390 _Self __tmp = *this;
391 _M_node = _Rb_tree_decrement(_M_node);
392 return __tmp;
393 }
394
e0b9bc23
FD
395 friend bool
396 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
397 { return __x._M_node == __y._M_node; }
e135a038 398
93843da6 399#if ! __cpp_lib_three_way_comparison
e0b9bc23
FD
400 friend bool
401 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
402 { return __x._M_node != __y._M_node; }
93843da6 403#endif
e135a038
BK
404
405 _Base_ptr _M_node;
737ab798 406 };
d3d526ac 407
ed6814f7 408 void
8bd22a3c 409 _Rb_tree_insert_and_rebalance(const bool __insert_left,
fee84d55
FD
410 _Rb_tree_node_base* __x,
411 _Rb_tree_node_base* __p,
412 _Rb_tree_node_base& __header) throw ();
ca1c7011
GB
413
414 _Rb_tree_node_base*
ed6814f7 415 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
1cf1c842 416 _Rb_tree_node_base& __header) throw ();
d3d526ac 417
2dbe56bd
JW
418#if __cplusplus > 201402L
419 template<typename _Tree1, typename _Cmp2>
420 struct _Rb_tree_merge_helper { };
421#endif
422
ed6814f7 423 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 424 typename _Compare, typename _Alloc = allocator<_Val> >
34c87829 425 class _Rb_tree
d3d526ac 426 {
ff90a89e 427 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
fee84d55 428 rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
ff90a89e
JW
429
430 typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
ed6814f7 431
d3d526ac 432 protected:
3b31a727
BK
433 typedef _Rb_tree_node_base* _Base_ptr;
434 typedef const _Rb_tree_node_base* _Const_Base_ptr;
c6195f58
FD
435 typedef _Rb_tree_node<_Val>* _Link_type;
436 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
437
438 private:
6c52b7df
FD
439 // Functor recycling a pool of nodes and using allocation once the pool
440 // is empty.
c6195f58
FD
441 struct _Reuse_or_alloc_node
442 {
6c52b7df 443 _Reuse_or_alloc_node(_Rb_tree& __t)
8b0cd47a 444 : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
c6195f58
FD
445 {
446 if (_M_root)
6c52b7df
FD
447 {
448 _M_root->_M_parent = 0;
449
450 if (_M_nodes->_M_left)
451 _M_nodes = _M_nodes->_M_left;
452 }
c6195f58
FD
453 else
454 _M_nodes = 0;
455 }
456
457#if __cplusplus >= 201103L
458 _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
459#endif
460
461 ~_Reuse_or_alloc_node()
462 { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
463
464 template<typename _Arg>
465 _Link_type
90bf60c3 466 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
c6195f58
FD
467 {
468 _Link_type __node = static_cast<_Link_type>(_M_extract());
469 if (__node)
470 {
471 _M_t._M_destroy_node(__node);
472 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
473 return __node;
474 }
475
476 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
477 }
478
479 private:
480 _Base_ptr
481 _M_extract()
482 {
483 if (!_M_nodes)
484 return _M_nodes;
485
486 _Base_ptr __node = _M_nodes;
487 _M_nodes = _M_nodes->_M_parent;
488 if (_M_nodes)
489 {
490 if (_M_nodes->_M_right == __node)
491 {
492 _M_nodes->_M_right = 0;
493
494 if (_M_nodes->_M_left)
495 {
496 _M_nodes = _M_nodes->_M_left;
497
498 while (_M_nodes->_M_right)
499 _M_nodes = _M_nodes->_M_right;
6c52b7df
FD
500
501 if (_M_nodes->_M_left)
502 _M_nodes = _M_nodes->_M_left;
c6195f58
FD
503 }
504 }
505 else // __node is on the left.
506 _M_nodes->_M_left = 0;
507 }
508 else
509 _M_root = 0;
510
511 return __node;
512 }
513
514 _Base_ptr _M_root;
515 _Base_ptr _M_nodes;
516 _Rb_tree& _M_t;
517 };
518
6c52b7df 519 // Functor similar to the previous one but without any pool of nodes to
c6195f58
FD
520 // recycle.
521 struct _Alloc_node
522 {
523 _Alloc_node(_Rb_tree& __t)
8b0cd47a 524 : _M_t(__t) { }
c6195f58
FD
525
526 template<typename _Arg>
527 _Link_type
90bf60c3 528 operator()(_GLIBCXX_FWDREF(_Arg) __arg) const
c6195f58
FD
529 { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
530
531 private:
532 _Rb_tree& _M_t;
533 };
ed6814f7 534
d3d526ac 535 public:
3b31a727
BK
536 typedef _Key key_type;
537 typedef _Val value_type;
538 typedef value_type* pointer;
539 typedef const value_type* const_pointer;
540 typedef value_type& reference;
541 typedef const value_type& const_reference;
3b31a727
BK
542 typedef size_t size_type;
543 typedef ptrdiff_t difference_type;
544 typedef _Alloc allocator_type;
8bd22a3c 545
31905f34 546 _Node_allocator&
d3677132 547 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
07cfc2d7 548 { return this->_M_impl; }
fee84d55 549
31905f34 550 const _Node_allocator&
d3677132 551 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
07cfc2d7 552 { return this->_M_impl; }
ed6814f7 553
31905f34 554 allocator_type
d3677132 555 get_allocator() const _GLIBCXX_NOEXCEPT
31905f34
PC
556 { return allocator_type(_M_get_Node_allocator()); }
557
d3d526ac 558 protected:
c3f824ce 559 _Link_type
62e67651 560 _M_get_node()
ff90a89e 561 { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
34c87829 562
ed6814f7 563 void
0e1a966a 564 _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
ff90a89e 565 { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
ed6814f7 566
734f5023 567#if __cplusplus < 201103L
c6195f58
FD
568 void
569 _M_construct_node(_Link_type __node, const value_type& __x)
d3d526ac 570 {
bc2631e0 571 __try
c6195f58 572 { get_allocator().construct(__node->_M_valptr(), __x); }
bc2631e0 573 __catch(...)
d3d526ac 574 {
c6195f58 575 _M_put_node(__node);
ed6814f7 576 __throw_exception_again;
d3d526ac 577 }
c6195f58
FD
578 }
579
580 _Link_type
581 _M_create_node(const value_type& __x)
582 {
583 _Link_type __tmp = _M_get_node();
584 _M_construct_node(__tmp, __x);
d3d526ac 585 return __tmp;
725dc051 586 }
25bbe9bc
PC
587#else
588 template<typename... _Args>
c6195f58
FD
589 void
590 _M_construct_node(_Link_type __node, _Args&&... __args)
25bbe9bc 591 {
bc2631e0 592 __try
25bbe9bc 593 {
c6195f58 594 ::new(__node) _Rb_tree_node<_Val>;
ff90a89e 595 _Alloc_traits::construct(_M_get_Node_allocator(),
c6195f58 596 __node->_M_valptr(),
ff90a89e 597 std::forward<_Args>(__args)...);
25bbe9bc 598 }
bc2631e0 599 __catch(...)
25bbe9bc 600 {
c6195f58
FD
601 __node->~_Rb_tree_node<_Val>();
602 _M_put_node(__node);
25bbe9bc
PC
603 __throw_exception_again;
604 }
c6195f58
FD
605 }
606
607 template<typename... _Args>
fee84d55
FD
608 _Link_type
609 _M_create_node(_Args&&... __args)
c6195f58
FD
610 {
611 _Link_type __tmp = _M_get_node();
612 _M_construct_node(__tmp, std::forward<_Args>(__args)...);
25bbe9bc
PC
613 return __tmp;
614 }
ca086dda 615#endif
25bbe9bc
PC
616
617 void
ca086dda 618 _M_destroy_node(_Link_type __p) _GLIBCXX_NOEXCEPT
25bbe9bc 619 {
ca086dda
JW
620#if __cplusplus < 201103L
621 get_allocator().destroy(__p->_M_valptr());
622#else
ff90a89e
JW
623 _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
624 __p->~_Rb_tree_node<_Val>();
25bbe9bc 625#endif
ca086dda 626 }
25bbe9bc 627
c6195f58
FD
628 void
629 _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
d3d526ac 630 {
c6195f58
FD
631 _M_destroy_node(__p);
632 _M_put_node(__p);
725dc051 633 }
d3d526ac 634
90bf60c3 635 template<bool _MoveValue, typename _NodeGen>
c6195f58 636 _Link_type
90bf60c3 637 _M_clone_node(_Link_type __x, _NodeGen& __node_gen)
c6195f58 638 {
90bf60c3 639#if __cplusplus >= 201103L
a09bb4a8
JW
640 using _Vp = __conditional_t<_MoveValue,
641 value_type&&,
642 const value_type&>;
90bf60c3
FD
643#endif
644 _Link_type __tmp
645 = __node_gen(_GLIBCXX_FORWARD(_Vp, *__x->_M_valptr()));
c6195f58
FD
646 __tmp->_M_color = __x->_M_color;
647 __tmp->_M_left = 0;
648 __tmp->_M_right = 0;
649 return __tmp;
650 }
651
34c87829 652 protected:
fee84d55
FD
653#if _GLIBCXX_INLINE_VERSION
654 template<typename _Key_compare>
655#else
c6195f58
FD
656 // Unused _Is_pod_comparator is kept as it is part of mangled name.
657 template<typename _Key_compare,
658 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
fee84d55
FD
659#endif
660 struct _Rb_tree_impl
a4dec0d6
FD
661 : public _Node_allocator
662 , public _Rb_tree_key_compare<_Key_compare>
663 , public _Rb_tree_header
fee84d55 664 {
a4dec0d6 665 typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
8bd22a3c 666
78b36b70 667 _Rb_tree_impl()
07cfc2d7
FD
668 _GLIBCXX_NOEXCEPT_IF(
669 is_nothrow_default_constructible<_Node_allocator>::value
670 && is_nothrow_default_constructible<_Base_key_compare>::value )
671 : _Node_allocator()
a4dec0d6 672 { }
a4dec0d6
FD
673
674 _Rb_tree_impl(const _Rb_tree_impl& __x)
675 : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x))
676 , _Base_key_compare(__x._M_key_compare)
eb6b71b8 677 , _Rb_tree_header()
a4dec0d6 678 { }
78b36b70 679
b9588893
FD
680#if __cplusplus < 201103L
681 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
682 : _Node_allocator(__a), _Base_key_compare(__comp)
683 { }
684#else
c832cf1c
FD
685 _Rb_tree_impl(_Rb_tree_impl&&)
686 noexcept( is_nothrow_move_constructible<_Base_key_compare>::value )
687 = default;
07cfc2d7 688
538a7cd0
FD
689 explicit
690 _Rb_tree_impl(_Node_allocator&& __a)
691 : _Node_allocator(std::move(__a))
692 { }
693
8b0cd47a
FD
694 _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
695 : _Node_allocator(std::move(__a)),
696 _Base_key_compare(std::move(__x)),
697 _Rb_tree_header(std::move(__x))
698 { }
699
6f59ea25 700 _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
a4dec0d6
FD
701 : _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
702 { }
6f59ea25 703#endif
8bd22a3c
BK
704 };
705
706 _Rb_tree_impl<_Compare> _M_impl;
ed6814f7 707
34c87829 708 protected:
b4c70e89 709 _Base_ptr&
0e1a966a 710 _M_root() _GLIBCXX_NOEXCEPT
8bd22a3c 711 { return this->_M_impl._M_header._M_parent; }
d3d526ac 712
b4c70e89 713 _Const_Base_ptr
0e1a966a 714 _M_root() const _GLIBCXX_NOEXCEPT
8bd22a3c 715 { return this->_M_impl._M_header._M_parent; }
d3d526ac 716
b4c70e89 717 _Base_ptr&
0e1a966a 718 _M_leftmost() _GLIBCXX_NOEXCEPT
8bd22a3c 719 { return this->_M_impl._M_header._M_left; }
b4c70e89
GB
720
721 _Const_Base_ptr
0e1a966a 722 _M_leftmost() const _GLIBCXX_NOEXCEPT
8bd22a3c 723 { return this->_M_impl._M_header._M_left; }
b4c70e89
GB
724
725 _Base_ptr&
0e1a966a 726 _M_rightmost() _GLIBCXX_NOEXCEPT
8bd22a3c 727 { return this->_M_impl._M_header._M_right; }
b4c70e89
GB
728
729 _Const_Base_ptr
0e1a966a 730 _M_rightmost() const _GLIBCXX_NOEXCEPT
8bd22a3c 731 { return this->_M_impl._M_header._M_right; }
d3d526ac 732
7f6dd1ca 733 _Link_type
90bf60c3 734 _M_mbegin() const _GLIBCXX_NOEXCEPT
8bd22a3c 735 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
b4c70e89 736
90bf60c3
FD
737 _Link_type
738 _M_begin() _GLIBCXX_NOEXCEPT
739 { return _M_mbegin(); }
740
b4c70e89 741 _Const_Link_type
0e1a966a 742 _M_begin() const _GLIBCXX_NOEXCEPT
11aaf40c
PC
743 {
744 return static_cast<_Const_Link_type>
745 (this->_M_impl._M_header._M_parent);
746 }
d3d526ac 747
151fbaac 748 _Base_ptr
0e1a966a 749 _M_end() _GLIBCXX_NOEXCEPT
151fbaac 750 { return &this->_M_impl._M_header; }
d3d526ac 751
151fbaac 752 _Const_Base_ptr
0e1a966a 753 _M_end() const _GLIBCXX_NOEXCEPT
151fbaac 754 { return &this->_M_impl._M_header; }
d3d526ac 755
ed6814f7 756 static const _Key&
62e67651 757 _S_key(_Const_Link_type __x)
e625ccc2
JW
758 {
759#if __cplusplus >= 201103L
760 // If we're asking for the key we're presumably using the comparison
761 // object, and so this is a good place to sanity check it.
762 static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
763 "comparison object must be invocable "
764 "with two arguments of key type");
765# if __cplusplus >= 201703L
766 // _GLIBCXX_RESOLVE_LIB_DEFECTS
767 // 2542. Missing const requirements for associative containers
768 if constexpr (__is_invocable<_Compare&, const _Key&, const _Key&>{})
769 static_assert(
770 is_invocable_v<const _Compare&, const _Key&, const _Key&>,
771 "comparison object must be invocable as const");
772# endif // C++17
773#endif // C++11
774
775 return _KeyOfValue()(*__x->_M_valptr());
776 }
b4c70e89
GB
777
778 static _Link_type
0e1a966a 779 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
62e67651 780 { return static_cast<_Link_type>(__x->_M_left); }
d3d526ac 781
b4c70e89 782 static _Const_Link_type
0e1a966a 783 _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
62e67651 784 { return static_cast<_Const_Link_type>(__x->_M_left); }
d3d526ac 785
b4c70e89 786 static _Link_type
0e1a966a 787 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
62e67651 788 { return static_cast<_Link_type>(__x->_M_right); }
d3d526ac 789
b4c70e89 790 static _Const_Link_type
0e1a966a 791 _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
62e67651 792 { return static_cast<_Const_Link_type>(__x->_M_right); }
d3d526ac 793
ed6814f7 794 static const _Key&
62e67651 795 _S_key(_Const_Base_ptr __x)
e625ccc2 796 { return _S_key(static_cast<_Const_Link_type>(__x)); }
b4c70e89 797
ed6814f7 798 static _Base_ptr
0e1a966a 799 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89 800 { return _Rb_tree_node_base::_S_minimum(__x); }
d3d526ac 801
b4c70e89 802 static _Const_Base_ptr
0e1a966a 803 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89 804 { return _Rb_tree_node_base::_S_minimum(__x); }
d3d526ac 805
b4c70e89 806 static _Base_ptr
0e1a966a 807 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89 808 { return _Rb_tree_node_base::_S_maximum(__x); }
d3d526ac 809
b4c70e89 810 static _Const_Base_ptr
0e1a966a 811 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
b4c70e89 812 { return _Rb_tree_node_base::_S_maximum(__x); }
d3d526ac
BK
813
814 public:
e135a038
BK
815 typedef _Rb_tree_iterator<value_type> iterator;
816 typedef _Rb_tree_const_iterator<value_type> const_iterator;
d3d526ac 817
e135a038 818 typedef std::reverse_iterator<iterator> reverse_iterator;
be26865d 819 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
d3d526ac 820
2dbe56bd
JW
821#if __cplusplus > 201402L
822 using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
85c05b5e 823 using insert_return_type = _Node_insert_return<
a09bb4a8 824 __conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
85c05b5e 825 node_type>;
2dbe56bd
JW
826#endif
827
55826ab6
FD
828 pair<_Base_ptr, _Base_ptr>
829 _M_get_insert_unique_pos(const key_type& __k);
830
831 pair<_Base_ptr, _Base_ptr>
832 _M_get_insert_equal_pos(const key_type& __k);
833
834 pair<_Base_ptr, _Base_ptr>
835 _M_get_insert_hint_unique_pos(const_iterator __pos,
836 const key_type& __k);
837
838 pair<_Base_ptr, _Base_ptr>
839 _M_get_insert_hint_equal_pos(const_iterator __pos,
840 const key_type& __k);
841
b95170d3 842 private:
734f5023 843#if __cplusplus >= 201103L
c6195f58 844 template<typename _Arg, typename _NodeGen>
fee84d55 845 iterator
c6195f58 846 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
55826ab6
FD
847
848 iterator
849 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
e6a05448
PC
850
851 template<typename _Arg>
fee84d55
FD
852 iterator
853 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
e6a05448
PC
854
855 template<typename _Arg>
fee84d55
FD
856 iterator
857 _M_insert_equal_lower(_Arg&& __x);
55826ab6
FD
858
859 iterator
860 _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
861
862 iterator
863 _M_insert_equal_lower_node(_Link_type __z);
e6a05448 864#else
c6195f58
FD
865 template<typename _NodeGen>
866 iterator
867 _M_insert_(_Base_ptr __x, _Base_ptr __y,
868 const value_type& __v, _NodeGen&);
d3d526ac 869
cf1e0371
PC
870 // _GLIBCXX_RESOLVE_LIB_DEFECTS
871 // 233. Insertion hints in associative containers.
872 iterator
55826ab6 873 _M_insert_lower(_Base_ptr __y, const value_type& __v);
cf1e0371 874
dc4871cb
PC
875 iterator
876 _M_insert_equal_lower(const value_type& __x);
e6a05448 877#endif
d5e07b79 878
90bf60c3
FD
879 enum { __as_lvalue, __as_rvalue };
880
881 template<bool _MoveValues, typename _NodeGen>
c6195f58 882 _Link_type
90bf60c3 883 _M_copy(_Link_type, _Base_ptr, _NodeGen&);
c6195f58 884
90bf60c3 885 template<bool _MoveValues, typename _NodeGen>
352f824f
FD
886 _Link_type
887 _M_copy(const _Rb_tree& __x, _NodeGen& __gen)
888 {
90bf60c3
FD
889 _Link_type __root =
890 _M_copy<_MoveValues>(__x._M_mbegin(), _M_end(), __gen);
352f824f
FD
891 _M_leftmost() = _S_minimum(__root);
892 _M_rightmost() = _S_maximum(__root);
893 _M_impl._M_node_count = __x._M_impl._M_node_count;
894 return __root;
895 }
896
ed6814f7 897 _Link_type
352f824f 898 _M_copy(const _Rb_tree& __x)
c6195f58
FD
899 {
900 _Alloc_node __an(*this);
90bf60c3 901 return _M_copy<__as_lvalue>(__x, __an);
c6195f58 902 }
d3d526ac 903
ed6814f7 904 void
d3d526ac
BK
905 _M_erase(_Link_type __x);
906
dc4871cb 907 iterator
151fbaac 908 _M_lower_bound(_Link_type __x, _Base_ptr __y,
f7e52577
PC
909 const _Key& __k);
910
911 const_iterator
151fbaac 912 _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
dc4871cb
PC
913 const _Key& __k) const;
914
915 iterator
151fbaac 916 _M_upper_bound(_Link_type __x, _Base_ptr __y,
f7e52577
PC
917 const _Key& __k);
918
919 const_iterator
151fbaac 920 _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
dc4871cb
PC
921 const _Key& __k) const;
922
d3d526ac
BK
923 public:
924 // allocation/deallocation
d72c3f0a 925#if __cplusplus < 201103L
78b36b70 926 _Rb_tree() { }
d72c3f0a
FD
927#else
928 _Rb_tree() = default;
929#endif
d3d526ac 930
78b36b70
PC
931 _Rb_tree(const _Compare& __comp,
932 const allocator_type& __a = allocator_type())
ff15f019 933 : _M_impl(__comp, _Node_allocator(__a)) { }
d3d526ac 934
78b36b70 935 _Rb_tree(const _Rb_tree& __x)
a4dec0d6 936 : _M_impl(__x._M_impl)
ed6814f7 937 {
8bd22a3c 938 if (__x._M_root() != 0)
352f824f 939 _M_root() = _M_copy(__x);
725dc051 940 }
d3d526ac 941
734f5023 942#if __cplusplus >= 201103L
ff90a89e 943 _Rb_tree(const allocator_type& __a)
538a7cd0 944 : _M_impl(_Node_allocator(__a))
ff90a89e
JW
945 { }
946
947 _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
948 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
949 {
c6195f58 950 if (__x._M_root() != nullptr)
352f824f 951 _M_root() = _M_copy(__x);
ff90a89e
JW
952 }
953
a4dec0d6 954 _Rb_tree(_Rb_tree&&) = default;
ff90a89e
JW
955
956 _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
957 : _Rb_tree(std::move(__x), _Node_allocator(__a))
958 { }
959
8b0cd47a
FD
960 private:
961 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
962 noexcept(is_nothrow_default_constructible<_Compare>::value)
963 : _M_impl(std::move(__x._M_impl), std::move(__a))
964 { }
965
966 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
967 : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
968 {
969 if (__x._M_root() != nullptr)
970 _M_move_data(__x, false_type{});
971 }
972
973 public:
974 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
975 noexcept( noexcept(
8b6d2d3b 976 _Rb_tree(std::declval<_Rb_tree&&>(), std::declval<_Node_allocator&&>(),
8b0cd47a
FD
977 std::declval<typename _Alloc_traits::is_always_equal>())) )
978 : _Rb_tree(std::move(__x), std::move(__a),
979 typename _Alloc_traits::is_always_equal{})
980 { }
053cc380
PC
981#endif
982
6f59ea25 983 ~_Rb_tree() _GLIBCXX_NOEXCEPT
e625ccc2 984 { _M_erase(_M_begin()); }
d3d526ac 985
78b36b70
PC
986 _Rb_tree&
987 operator=(const _Rb_tree& __x);
d3d526ac 988
d3d526ac 989 // Accessors.
ed6814f7 990 _Compare
62e67651 991 key_comp() const
8bd22a3c 992 { return _M_impl._M_key_compare; }
d3d526ac 993
ed6814f7 994 iterator
d3677132 995 begin() _GLIBCXX_NOEXCEPT
2097b5b0 996 { return iterator(this->_M_impl._M_header._M_left); }
d3d526ac 997
ed6814f7 998 const_iterator
d3677132 999 begin() const _GLIBCXX_NOEXCEPT
2097b5b0 1000 { return const_iterator(this->_M_impl._M_header._M_left); }
d3d526ac 1001
ed6814f7 1002 iterator
d3677132 1003 end() _GLIBCXX_NOEXCEPT
2097b5b0 1004 { return iterator(&this->_M_impl._M_header); }
d3d526ac 1005
7f6dd1ca 1006 const_iterator
d3677132 1007 end() const _GLIBCXX_NOEXCEPT
2097b5b0 1008 { return const_iterator(&this->_M_impl._M_header); }
d3d526ac 1009
ed6814f7 1010 reverse_iterator
d3677132 1011 rbegin() _GLIBCXX_NOEXCEPT
62e67651 1012 { return reverse_iterator(end()); }
d3d526ac 1013
ed6814f7 1014 const_reverse_iterator
d3677132 1015 rbegin() const _GLIBCXX_NOEXCEPT
62e67651 1016 { return const_reverse_iterator(end()); }
d3d526ac 1017
ed6814f7 1018 reverse_iterator
d3677132 1019 rend() _GLIBCXX_NOEXCEPT
62e67651 1020 { return reverse_iterator(begin()); }
d3d526ac 1021
ed6814f7 1022 const_reverse_iterator
d3677132 1023 rend() const _GLIBCXX_NOEXCEPT
62e67651 1024 { return const_reverse_iterator(begin()); }
ed6814f7 1025
d715f554 1026 _GLIBCXX_NODISCARD bool
d3677132 1027 empty() const _GLIBCXX_NOEXCEPT
8bd22a3c 1028 { return _M_impl._M_node_count == 0; }
d3d526ac 1029
ed6814f7 1030 size_type
fee84d55 1031 size() const _GLIBCXX_NOEXCEPT
8bd22a3c 1032 { return _M_impl._M_node_count; }
d3d526ac 1033
ed6814f7 1034 size_type
d3677132 1035 max_size() const _GLIBCXX_NOEXCEPT
ff90a89e 1036 { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
d3d526ac 1037
ed6814f7 1038 void
c5d9ec56
JW
1039 swap(_Rb_tree& __t)
1040 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
ed6814f7 1041
d3d526ac 1042 // Insert/erase.
734f5023 1043#if __cplusplus >= 201103L
e6a05448 1044 template<typename _Arg>
fee84d55
FD
1045 pair<iterator, bool>
1046 _M_insert_unique(_Arg&& __x);
e6a05448
PC
1047
1048 template<typename _Arg>
fee84d55
FD
1049 iterator
1050 _M_insert_equal(_Arg&& __x);
e6a05448 1051
c6195f58 1052 template<typename _Arg, typename _NodeGen>
fee84d55 1053 iterator
c6195f58 1054 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
e6a05448
PC
1055
1056 template<typename _Arg>
c6195f58
FD
1057 iterator
1058 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
1059 {
1060 _Alloc_node __an(*this);
1061 return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
1062 }
1063
1064 template<typename _Arg, typename _NodeGen>
1065 iterator
1066 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1067
1068 template<typename _Arg>
1069 iterator
1070 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
1071 {
1072 _Alloc_node __an(*this);
1073 return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
1074 }
55826ab6
FD
1075
1076 template<typename... _Args>
1077 pair<iterator, bool>
1078 _M_emplace_unique(_Args&&... __args);
1079
1080 template<typename... _Args>
1081 iterator
1082 _M_emplace_equal(_Args&&... __args);
1083
1084 template<typename... _Args>
1085 iterator
1086 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
1087
1088 template<typename... _Args>
1089 iterator
1090 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
83a840a9
JW
1091
1092 template<typename _Iter>
1093 using __same_value_type
1094 = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
1095
1096 template<typename _InputIterator>
1097 __enable_if_t<__same_value_type<_InputIterator>::value>
1098 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1099 {
1100 _Alloc_node __an(*this);
1101 for (; __first != __last; ++__first)
1102 _M_insert_unique_(end(), *__first, __an);
1103 }
1104
1105 template<typename _InputIterator>
1106 __enable_if_t<!__same_value_type<_InputIterator>::value>
1107 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1108 {
1109 for (; __first != __last; ++__first)
1110 _M_emplace_unique(*__first);
1111 }
1112
1113 template<typename _InputIterator>
1114 __enable_if_t<__same_value_type<_InputIterator>::value>
1115 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1116 {
1117 _Alloc_node __an(*this);
1118 for (; __first != __last; ++__first)
1119 _M_insert_equal_(end(), *__first, __an);
1120 }
1121
1122 template<typename _InputIterator>
1123 __enable_if_t<!__same_value_type<_InputIterator>::value>
1124 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1125 {
83a840a9
JW
1126 for (; __first != __last; ++__first)
1127 _M_emplace_equal(*__first);
1128 }
e6a05448 1129#else
42a27024
PC
1130 pair<iterator, bool>
1131 _M_insert_unique(const value_type& __x);
d3d526ac 1132
ed6814f7 1133 iterator
42a27024 1134 _M_insert_equal(const value_type& __x);
d3d526ac 1135
c6195f58
FD
1136 template<typename _NodeGen>
1137 iterator
1138 _M_insert_unique_(const_iterator __pos, const value_type& __x,
1139 _NodeGen&);
1140
ed6814f7 1141 iterator
c6195f58
FD
1142 _M_insert_unique_(const_iterator __pos, const value_type& __x)
1143 {
1144 _Alloc_node __an(*this);
1145 return _M_insert_unique_(__pos, __x, __an);
1146 }
d5e07b79 1147
c6195f58
FD
1148 template<typename _NodeGen>
1149 iterator
1150 _M_insert_equal_(const_iterator __pos, const value_type& __x,
1151 _NodeGen&);
ed6814f7 1152 iterator
c6195f58
FD
1153 _M_insert_equal_(const_iterator __pos, const value_type& __x)
1154 {
1155 _Alloc_node __an(*this);
1156 return _M_insert_equal_(__pos, __x, __an);
1157 }
d5e07b79 1158
d3d526ac 1159 template<typename _InputIterator>
fee84d55 1160 void
83a840a9
JW
1161 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1162 {
1163 _Alloc_node __an(*this);
1164 for (; __first != __last; ++__first)
1165 _M_insert_unique_(end(), *__first, __an);
1166 }
d3d526ac
BK
1167
1168 template<typename _InputIterator>
fee84d55 1169 void
83a840a9
JW
1170 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1171 {
1172 _Alloc_node __an(*this);
1173 for (; __first != __last; ++__first)
1174 _M_insert_equal_(end(), *__first, __an);
1175 }
1176#endif
d3d526ac 1177
7606bd11
PC
1178 private:
1179 void
1180 _M_erase_aux(const_iterator __position);
1181
1182 void
1183 _M_erase_aux(const_iterator __first, const_iterator __last);
1184
1185 public:
734f5023 1186#if __cplusplus >= 201103L
c105751c
ESR
1187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1188 // DR 130. Associative erase should return an iterator.
3b31a727 1189 _GLIBCXX_ABI_TAG_CXX11
c105751c 1190 iterator
7606bd11
PC
1191 erase(const_iterator __position)
1192 {
36b88526 1193 __glibcxx_assert(__position != end());
7606bd11
PC
1194 const_iterator __result = __position;
1195 ++__result;
1196 _M_erase_aux(__position);
6b6d5d09 1197 return __result._M_const_cast();
7606bd11 1198 }
6dc88283
PC
1199
1200 // LWG 2059.
3b31a727 1201 _GLIBCXX_ABI_TAG_CXX11
6dc88283
PC
1202 iterator
1203 erase(iterator __position)
1204 {
36b88526 1205 __glibcxx_assert(__position != end());
6dc88283
PC
1206 iterator __result = __position;
1207 ++__result;
1208 _M_erase_aux(__position);
1209 return __result;
1210 }
c105751c 1211#else
03e38c1a
PC
1212 void
1213 erase(iterator __position)
36b88526
JW
1214 {
1215 __glibcxx_assert(__position != end());
1216 _M_erase_aux(__position);
1217 }
03e38c1a 1218
ed6814f7 1219 void
7606bd11 1220 erase(const_iterator __position)
36b88526
JW
1221 {
1222 __glibcxx_assert(__position != end());
1223 _M_erase_aux(__position);
1224 }
c105751c 1225#endif
7198e982 1226
ed6814f7 1227 size_type
d3d526ac
BK
1228 erase(const key_type& __x);
1229
734f5023 1230#if __cplusplus >= 201103L
c105751c
ESR
1231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1232 // DR 130. Associative erase should return an iterator.
3b31a727 1233 _GLIBCXX_ABI_TAG_CXX11
c105751c 1234 iterator
7606bd11
PC
1235 erase(const_iterator __first, const_iterator __last)
1236 {
1237 _M_erase_aux(__first, __last);
6b6d5d09 1238 return __last._M_const_cast();
7606bd11 1239 }
c105751c 1240#else
03e38c1a
PC
1241 void
1242 erase(iterator __first, iterator __last)
1243 { _M_erase_aux(__first, __last); }
1244
ed6814f7 1245 void
7606bd11
PC
1246 erase(const_iterator __first, const_iterator __last)
1247 { _M_erase_aux(__first, __last); }
c105751c 1248#endif
d3d526ac 1249
e135a038 1250 void
d3677132 1251 clear() _GLIBCXX_NOEXCEPT
d3d526ac 1252 {
fee84d55 1253 _M_erase(_M_begin());
c6195f58 1254 _M_impl._M_reset();
e135a038 1255 }
d3d526ac
BK
1256
1257 // Set operations.
ed6814f7 1258 iterator
dc4871cb 1259 find(const key_type& __k);
d3d526ac 1260
ed6814f7 1261 const_iterator
dc4871cb 1262 find(const key_type& __k) const;
d3d526ac 1263
ed6814f7 1264 size_type
dc4871cb 1265 count(const key_type& __k) const;
d3d526ac 1266
ed6814f7 1267 iterator
dc4871cb 1268 lower_bound(const key_type& __k)
f7e52577 1269 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
d3d526ac 1270
ed6814f7 1271 const_iterator
dc4871cb 1272 lower_bound(const key_type& __k) const
f7e52577 1273 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
d3d526ac 1274
ed6814f7 1275 iterator
dc4871cb 1276 upper_bound(const key_type& __k)
f7e52577 1277 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
d3d526ac 1278
ed6814f7 1279 const_iterator
dc4871cb 1280 upper_bound(const key_type& __k) const
f7e52577 1281 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
d3d526ac 1282
dc4871cb 1283 pair<iterator, iterator>
f7e52577 1284 equal_range(const key_type& __k);
d3d526ac 1285
ed6814f7 1286 pair<const_iterator, const_iterator>
f7e52577 1287 equal_range(const key_type& __k) const;
d3d526ac 1288
3adea09e 1289#if __cplusplus >= 201402L
91c78ea5 1290 template<typename _Kt,
3adea09e 1291 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1292 iterator
1293 _M_find_tr(const _Kt& __k)
1294 {
151fbaac
JW
1295 const _Rb_tree* __const_this = this;
1296 return __const_this->_M_find_tr(__k)._M_const_cast();
91c78ea5
JW
1297 }
1298
1299 template<typename _Kt,
3adea09e 1300 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1301 const_iterator
1302 _M_find_tr(const _Kt& __k) const
1303 {
151fbaac
JW
1304 auto __j = _M_lower_bound_tr(__k);
1305 if (__j != end() && _M_impl._M_key_compare(__k, _S_key(__j._M_node)))
1306 __j = end();
1307 return __j;
91c78ea5
JW
1308 }
1309
1310 template<typename _Kt,
3adea09e 1311 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1312 size_type
1313 _M_count_tr(const _Kt& __k) const
1314 {
1315 auto __p = _M_equal_range_tr(__k);
1316 return std::distance(__p.first, __p.second);
1317 }
1318
1319 template<typename _Kt,
3adea09e 1320 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1321 iterator
1322 _M_lower_bound_tr(const _Kt& __k)
1323 {
151fbaac
JW
1324 const _Rb_tree* __const_this = this;
1325 return __const_this->_M_lower_bound_tr(__k)._M_const_cast();
91c78ea5
JW
1326 }
1327
1328 template<typename _Kt,
3adea09e 1329 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1330 const_iterator
1331 _M_lower_bound_tr(const _Kt& __k) const
1332 {
151fbaac
JW
1333 auto __x = _M_begin();
1334 auto __y = _M_end();
1335 while (__x != 0)
1336 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1337 {
1338 __y = __x;
1339 __x = _S_left(__x);
1340 }
1341 else
1342 __x = _S_right(__x);
1343 return const_iterator(__y);
91c78ea5
JW
1344 }
1345
1346 template<typename _Kt,
3adea09e 1347 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1348 iterator
1349 _M_upper_bound_tr(const _Kt& __k)
1350 {
151fbaac
JW
1351 const _Rb_tree* __const_this = this;
1352 return __const_this->_M_upper_bound_tr(__k)._M_const_cast();
91c78ea5
JW
1353 }
1354
1355 template<typename _Kt,
3adea09e 1356 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1357 const_iterator
1358 _M_upper_bound_tr(const _Kt& __k) const
1359 {
151fbaac
JW
1360 auto __x = _M_begin();
1361 auto __y = _M_end();
1362 while (__x != 0)
1363 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1364 {
1365 __y = __x;
1366 __x = _S_left(__x);
1367 }
1368 else
1369 __x = _S_right(__x);
1370 return const_iterator(__y);
91c78ea5
JW
1371 }
1372
1373 template<typename _Kt,
3adea09e 1374 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1375 pair<iterator, iterator>
1376 _M_equal_range_tr(const _Kt& __k)
1377 {
151fbaac
JW
1378 const _Rb_tree* __const_this = this;
1379 auto __ret = __const_this->_M_equal_range_tr(__k);
1380 return { __ret.first._M_const_cast(), __ret.second._M_const_cast() };
91c78ea5
JW
1381 }
1382
1383 template<typename _Kt,
3adea09e 1384 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
91c78ea5
JW
1385 pair<const_iterator, const_iterator>
1386 _M_equal_range_tr(const _Kt& __k) const
1387 {
1388 auto __low = _M_lower_bound_tr(__k);
1389 auto __high = __low;
1390 auto& __cmp = _M_impl._M_key_compare;
1391 while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
1392 ++__high;
1393 return { __low, __high };
1394 }
1395#endif
1396
d3d526ac 1397 // Debugging.
ed6814f7 1398 bool
d3d526ac 1399 __rb_verify() const;
ff90a89e
JW
1400
1401#if __cplusplus >= 201103L
c6195f58 1402 _Rb_tree&
a2b5fdcb
JW
1403 operator=(_Rb_tree&&)
1404 noexcept(_Alloc_traits::_S_nothrow_move()
1405 && is_nothrow_move_assignable<_Compare>::value);
c6195f58
FD
1406
1407 template<typename _Iterator>
1408 void
1409 _M_assign_unique(_Iterator, _Iterator);
1410
1411 template<typename _Iterator>
1412 void
1413 _M_assign_equal(_Iterator, _Iterator);
6a5839c8
JW
1414
1415 private:
1416 // Move elements from container with equal allocator.
1417 void
8b0cd47a 1418 _M_move_data(_Rb_tree& __x, true_type)
a4dec0d6 1419 { _M_impl._M_move_data(__x._M_impl); }
6a5839c8
JW
1420
1421 // Move elements from container with possibly non-equal allocator,
1422 // which might result in a copy not a move.
1423 void
8b0cd47a 1424 _M_move_data(_Rb_tree&, false_type);
5ea387db
JW
1425
1426 // Move assignment from container with equal allocator.
1427 void
8b0cd47a 1428 _M_move_assign(_Rb_tree&, true_type);
5ea387db
JW
1429
1430 // Move assignment from container with possibly non-equal allocator,
1431 // which might result in a copy not a move.
1432 void
8b0cd47a 1433 _M_move_assign(_Rb_tree&, false_type);
ff90a89e 1434#endif
2dbe56bd
JW
1435
1436#if __cplusplus > 201402L
1437 public:
1438 /// Re-insert an extracted node.
1439 insert_return_type
1440 _M_reinsert_node_unique(node_type&& __nh)
1441 {
1442 insert_return_type __ret;
1443 if (__nh.empty())
1444 __ret.position = end();
1445 else
1446 {
1447 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1448
1449 auto __res = _M_get_insert_unique_pos(__nh._M_key());
1450 if (__res.second)
1451 {
1452 __ret.position
1453 = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1454 __nh._M_ptr = nullptr;
1455 __ret.inserted = true;
1456 }
1457 else
1458 {
1459 __ret.node = std::move(__nh);
1460 __ret.position = iterator(__res.first);
1461 __ret.inserted = false;
1462 }
1463 }
1464 return __ret;
1465 }
1466
1467 /// Re-insert an extracted node.
1468 iterator
1469 _M_reinsert_node_equal(node_type&& __nh)
1470 {
1471 iterator __ret;
1472 if (__nh.empty())
1473 __ret = end();
1474 else
1475 {
1476 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1477 auto __res = _M_get_insert_equal_pos(__nh._M_key());
1478 if (__res.second)
1479 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1480 else
1481 __ret = _M_insert_equal_lower_node(__nh._M_ptr);
1482 __nh._M_ptr = nullptr;
1483 }
1484 return __ret;
1485 }
1486
1487 /// Re-insert an extracted node.
1488 iterator
1489 _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
1490 {
1491 iterator __ret;
1492 if (__nh.empty())
1493 __ret = end();
1494 else
1495 {
1496 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1497 auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
1498 if (__res.second)
1499 {
1500 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1501 __nh._M_ptr = nullptr;
1502 }
1503 else
1504 __ret = iterator(__res.first);
1505 }
1506 return __ret;
1507 }
1508
1509 /// Re-insert an extracted node.
1510 iterator
1511 _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
1512 {
1513 iterator __ret;
1514 if (__nh.empty())
1515 __ret = end();
1516 else
1517 {
1518 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1519 auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
1520 if (__res.second)
1521 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1522 else
1523 __ret = _M_insert_equal_lower_node(__nh._M_ptr);
1524 __nh._M_ptr = nullptr;
1525 }
1526 return __ret;
1527 }
1528
1529 /// Extract a node.
1530 node_type
1531 extract(const_iterator __pos)
1532 {
1533 auto __ptr = _Rb_tree_rebalance_for_erase(
1534 __pos._M_const_cast()._M_node, _M_impl._M_header);
1535 --_M_impl._M_node_count;
1536 return { static_cast<_Link_type>(__ptr), _M_get_Node_allocator() };
1537 }
1538
1539 /// Extract a node.
1540 node_type
1541 extract(const key_type& __k)
1542 {
1543 node_type __nh;
1544 auto __pos = find(__k);
1545 if (__pos != end())
1546 __nh = extract(const_iterator(__pos));
1547 return __nh;
1548 }
1549
1550 template<typename _Compare2>
1551 using _Compatible_tree
1552 = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
1553
1554 template<typename, typename>
1555 friend class _Rb_tree_merge_helper;
1556
1557 /// Merge from a compatible container into one with unique keys.
1558 template<typename _Compare2>
1559 void
1560 _M_merge_unique(_Compatible_tree<_Compare2>& __src) noexcept
1561 {
1562 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
1563 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1564 {
1565 auto __pos = __i++;
1566 auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
1567 if (__res.second)
1568 {
1569 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
1570 auto __ptr = _Rb_tree_rebalance_for_erase(
1571 __pos._M_node, __src_impl._M_header);
1572 --__src_impl._M_node_count;
1573 _M_insert_node(__res.first, __res.second,
1574 static_cast<_Link_type>(__ptr));
1575 }
1576 }
1577 }
1578
1579 /// Merge from a compatible container into one with equivalent keys.
1580 template<typename _Compare2>
1581 void
1582 _M_merge_equal(_Compatible_tree<_Compare2>& __src) noexcept
1583 {
1584 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
1585 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1586 {
1587 auto __pos = __i++;
1588 auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
1589 if (__res.second)
1590 {
1591 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
1592 auto __ptr = _Rb_tree_rebalance_for_erase(
1593 __pos._M_node, __src_impl._M_header);
1594 --__src_impl._M_node_count;
1595 _M_insert_node(__res.first, __res.second,
1596 static_cast<_Link_type>(__ptr));
1597 }
1598 }
1599 }
1600#endif // C++17
d3d526ac 1601
e0b9bc23
FD
1602 friend bool
1603 operator==(const _Rb_tree& __x, const _Rb_tree& __y)
1604 {
1605 return __x.size() == __y.size()
1606 && std::equal(__x.begin(), __x.end(), __y.begin());
1607 }
d3d526ac 1608
93843da6
JW
1609#if __cpp_lib_three_way_comparison
1610 friend auto
1611 operator<=>(const _Rb_tree& __x, const _Rb_tree& __y)
1612 {
1613 if constexpr (requires { typename __detail::__synth3way_t<_Val>; })
1614 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
1615 __y.begin(), __y.end(),
1616 __detail::__synth3way);
1617 }
1618#else
e0b9bc23
FD
1619 friend bool
1620 operator<(const _Rb_tree& __x, const _Rb_tree& __y)
1621 {
1622 return std::lexicographical_compare(__x.begin(), __x.end(),
1623 __y.begin(), __y.end());
1624 }
93843da6 1625#endif
bf548ce3
JW
1626
1627 private:
1628#if __cplusplus >= 201103L
1629 // An RAII _Node handle
1630 struct _Auto_node
1631 {
1632 template<typename... _Args>
1633 _Auto_node(_Rb_tree& __t, _Args&&... __args)
1634 : _M_t(__t),
1635 _M_node(__t._M_create_node(std::forward<_Args>(__args)...))
1636 { }
1637
1638 ~_Auto_node()
1639 {
1640 if (_M_node)
1641 _M_t._M_drop_node(_M_node);
1642 }
1643
1644 _Auto_node(_Auto_node&& __n)
1645 : _M_t(__n._M_t), _M_node(__n._M_node)
1646 { __n._M_node = nullptr; }
1647
1648 const _Key&
1649 _M_key() const
1650 { return _S_key(_M_node); }
1651
1652 iterator
1653 _M_insert(pair<_Base_ptr, _Base_ptr> __p)
1654 {
1655 auto __it = _M_t._M_insert_node(__p.first, __p.second, _M_node);
1656 _M_node = nullptr;
1657 return __it;
1658 }
1659
1660 iterator
1661 _M_insert_equal_lower()
1662 {
1663 auto __it = _M_t._M_insert_equal_lower_node(_M_node);
1664 _M_node = nullptr;
1665 return __it;
1666 }
1667
1668 _Rb_tree& _M_t;
1669 _Link_type _M_node;
1670 };
1671#endif // C++11
e0b9bc23 1672 };
d3d526ac 1673
ed6814f7 1674 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1675 typename _Compare, typename _Alloc>
ed6814f7 1676 inline void
11aaf40c
PC
1677 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1678 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
d3d526ac
BK
1679 { __x.swap(__y); }
1680
734f5023 1681#if __cplusplus >= 201103L
6a5839c8 1682 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1683 typename _Compare, typename _Alloc>
6a5839c8
JW
1684 void
1685 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
8b0cd47a 1686 _M_move_data(_Rb_tree& __x, false_type)
6a5839c8
JW
1687 {
1688 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
8b0cd47a 1689 _M_move_data(__x, true_type());
6a5839c8
JW
1690 else
1691 {
05655141 1692 constexpr bool __move = !__move_if_noexcept_cond<value_type>::value;
c6195f58 1693 _Alloc_node __an(*this);
05655141
JW
1694 _M_root() = _M_copy<__move>(__x, __an);
1695 if _GLIBCXX17_CONSTEXPR (__move)
1696 __x.clear();
ff90a89e
JW
1697 }
1698 }
1699
1700 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1701 typename _Compare, typename _Alloc>
5ea387db 1702 inline void
ff90a89e 1703 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
5ea387db 1704 _M_move_assign(_Rb_tree& __x, true_type)
ff90a89e 1705 {
5ea387db
JW
1706 clear();
1707 if (__x._M_root() != nullptr)
8b0cd47a 1708 _M_move_data(__x, true_type());
5ea387db
JW
1709 std::__alloc_on_move(_M_get_Node_allocator(),
1710 __x._M_get_Node_allocator());
1711 }
1712
1713 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1714 typename _Compare, typename _Alloc>
5ea387db
JW
1715 void
1716 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1717 _M_move_assign(_Rb_tree& __x, false_type)
1718 {
1719 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
1720 return _M_move_assign(__x, true_type{});
c6195f58
FD
1721
1722 // Try to move each node reusing existing nodes and copying __x nodes
1723 // structure.
6c52b7df 1724 _Reuse_or_alloc_node __roan(*this);
c6195f58
FD
1725 _M_impl._M_reset();
1726 if (__x._M_root() != nullptr)
1727 {
90bf60c3 1728 _M_root() = _M_copy<__as_rvalue>(__x, __roan);
c6195f58 1729 __x.clear();
053cc380 1730 }
5ea387db
JW
1731 }
1732
1733 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1734 typename _Compare, typename _Alloc>
5ea387db
JW
1735 inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1736 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1737 operator=(_Rb_tree&& __x)
1738 noexcept(_Alloc_traits::_S_nothrow_move()
1739 && is_nothrow_move_assignable<_Compare>::value)
1740 {
e46d22a8 1741 _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
352f824f 1742 _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>());
c6195f58 1743 return *this;
053cc380 1744 }
c6195f58
FD
1745
1746 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1747 typename _Compare, typename _Alloc>
c6195f58
FD
1748 template<typename _Iterator>
1749 void
1750 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1751 _M_assign_unique(_Iterator __first, _Iterator __last)
1752 {
6c52b7df 1753 _Reuse_or_alloc_node __roan(*this);
c6195f58
FD
1754 _M_impl._M_reset();
1755 for (; __first != __last; ++__first)
1756 _M_insert_unique_(end(), *__first, __roan);
1757 }
1758
1759 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1760 typename _Compare, typename _Alloc>
c6195f58
FD
1761 template<typename _Iterator>
1762 void
1763 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1764 _M_assign_equal(_Iterator __first, _Iterator __last)
1765 {
6c52b7df 1766 _Reuse_or_alloc_node __roan(*this);
c6195f58
FD
1767 _M_impl._M_reset();
1768 for (; __first != __last; ++__first)
1769 _M_insert_equal_(end(), *__first, __roan);
1770 }
053cc380
PC
1771#endif
1772
ed6814f7 1773 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1774 typename _Compare, typename _Alloc>
11aaf40c
PC
1775 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1776 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
ff90a89e 1777 operator=(const _Rb_tree& __x)
d3d526ac 1778 {
47915ef8 1779 if (this != std::__addressof(__x))
d3d526ac
BK
1780 {
1781 // Note that _Key may be a constant type.
ff90a89e
JW
1782#if __cplusplus >= 201103L
1783 if (_Alloc_traits::_S_propagate_on_copy_assign())
1784 {
1785 auto& __this_alloc = this->_M_get_Node_allocator();
1786 auto& __that_alloc = __x._M_get_Node_allocator();
1787 if (!_Alloc_traits::_S_always_equal()
1788 && __this_alloc != __that_alloc)
1789 {
c6195f58
FD
1790 // Replacement allocator cannot free existing storage, we need
1791 // to erase nodes first.
1792 clear();
ff90a89e
JW
1793 std::__alloc_on_copy(__this_alloc, __that_alloc);
1794 }
1795 }
1796#endif
c6195f58 1797
6c52b7df 1798 _Reuse_or_alloc_node __roan(*this);
c6195f58 1799 _M_impl._M_reset();
8bd22a3c 1800 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
ed6814f7 1801 if (__x._M_root() != 0)
90bf60c3 1802 _M_root() = _M_copy<__as_lvalue>(__x, __roan);
d3d526ac 1803 }
c6195f58 1804
d3d526ac 1805 return *this;
725dc051 1806 }
d3d526ac 1807
ed6814f7 1808 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1809 typename _Compare, typename _Alloc>
734f5023 1810#if __cplusplus >= 201103L
c6195f58
FD
1811 template<typename _Arg, typename _NodeGen>
1812#else
1813 template<typename _NodeGen>
e6a05448 1814#endif
c6195f58
FD
1815 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1816 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1817 _M_insert_(_Base_ptr __x, _Base_ptr __p,
734f5023 1818#if __cplusplus >= 201103L
c6195f58 1819 _Arg&& __v,
e6a05448 1820#else
c6195f58 1821 const _Val& __v,
e6a05448 1822#endif
c6195f58
FD
1823 _NodeGen& __node_gen)
1824 {
1825 bool __insert_left = (__x != 0 || __p == _M_end()
1826 || _M_impl._M_key_compare(_KeyOfValue()(__v),
1827 _S_key(__p)));
e135a038 1828
c6195f58 1829 _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
e135a038 1830
c6195f58
FD
1831 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1832 this->_M_impl._M_header);
1833 ++_M_impl._M_node_count;
1834 return iterator(__z);
1835 }
d3d526ac 1836
cf1e0371 1837 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1838 typename _Compare, typename _Alloc>
734f5023 1839#if __cplusplus >= 201103L
e6a05448
PC
1840 template<typename _Arg>
1841#endif
cf1e0371
PC
1842 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1843 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
734f5023 1844#if __cplusplus >= 201103L
55826ab6 1845 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
e6a05448 1846#else
55826ab6 1847 _M_insert_lower(_Base_ptr __p, const _Val& __v)
e6a05448 1848#endif
cf1e0371 1849 {
55826ab6 1850 bool __insert_left = (__p == _M_end()
cf1e0371
PC
1851 || !_M_impl._M_key_compare(_S_key(__p),
1852 _KeyOfValue()(__v)));
1853
e6a05448 1854 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
cf1e0371 1855
55826ab6 1856 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
cf1e0371
PC
1857 this->_M_impl._M_header);
1858 ++_M_impl._M_node_count;
1859 return iterator(__z);
1860 }
1861
d5e07b79 1862 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1863 typename _Compare, typename _Alloc>
734f5023 1864#if __cplusplus >= 201103L
e6a05448
PC
1865 template<typename _Arg>
1866#endif
dc4871cb 1867 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
d5e07b79 1868 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
734f5023 1869#if __cplusplus >= 201103L
e6a05448
PC
1870 _M_insert_equal_lower(_Arg&& __v)
1871#else
dc4871cb 1872 _M_insert_equal_lower(const _Val& __v)
e6a05448 1873#endif
d5e07b79 1874 {
dc4871cb 1875 _Link_type __x = _M_begin();
151fbaac 1876 _Base_ptr __y = _M_end();
dc4871cb
PC
1877 while (__x != 0)
1878 {
1879 __y = __x;
1880 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
fee84d55 1881 _S_left(__x) : _S_right(__x);
dc4871cb 1882 }
55826ab6 1883 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
dc4871cb 1884 }
d5e07b79 1885
dc4871cb 1886 template<typename _Key, typename _Val, typename _KoV,
c6195f58 1887 typename _Compare, typename _Alloc>
90bf60c3 1888 template<bool _MoveValues, typename _NodeGen>
c6195f58
FD
1889 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1890 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
90bf60c3 1891 _M_copy(_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen)
c6195f58
FD
1892 {
1893 // Structural copy. __x and __p must be non-null.
90bf60c3 1894 _Link_type __top = _M_clone_node<_MoveValues>(__x, __node_gen);
c6195f58 1895 __top->_M_parent = __p;
dc4871cb 1896
c6195f58
FD
1897 __try
1898 {
1899 if (__x->_M_right)
90bf60c3
FD
1900 __top->_M_right =
1901 _M_copy<_MoveValues>(_S_right(__x), __top, __node_gen);
c6195f58
FD
1902 __p = __top;
1903 __x = _S_left(__x);
1904
1905 while (__x != 0)
1906 {
90bf60c3 1907 _Link_type __y = _M_clone_node<_MoveValues>(__x, __node_gen);
c6195f58
FD
1908 __p->_M_left = __y;
1909 __y->_M_parent = __p;
1910 if (__x->_M_right)
90bf60c3
FD
1911 __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
1912 __y, __node_gen);
c6195f58
FD
1913 __p = __y;
1914 __x = _S_left(__x);
1915 }
1916 }
1917 __catch(...)
1918 {
1919 _M_erase(__top);
1920 __throw_exception_again;
1921 }
1922 return __top;
1923 }
d5e07b79 1924
ed6814f7 1925 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1926 typename _Compare, typename _Alloc>
dc4871cb 1927 void
11aaf40c 1928 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
dc4871cb 1929 _M_erase(_Link_type __x)
d3d526ac 1930 {
dc4871cb 1931 // Erase without rebalancing.
ed6814f7 1932 while (__x != 0)
d3d526ac 1933 {
dc4871cb
PC
1934 _M_erase(_S_right(__x));
1935 _Link_type __y = _S_left(__x);
c6195f58 1936 _M_drop_node(__x);
dc4871cb 1937 __x = __y;
d3d526ac 1938 }
d3d526ac
BK
1939 }
1940
cf1e0371 1941 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1942 typename _Compare, typename _Alloc>
f7e52577
PC
1943 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1944 _Compare, _Alloc>::iterator
1945 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
151fbaac 1946 _M_lower_bound(_Link_type __x, _Base_ptr __y,
f7e52577
PC
1947 const _Key& __k)
1948 {
1949 while (__x != 0)
1950 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1951 __y = __x, __x = _S_left(__x);
1952 else
1953 __x = _S_right(__x);
1954 return iterator(__y);
1955 }
1956
1957 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1958 typename _Compare, typename _Alloc>
f7e52577
PC
1959 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1960 _Compare, _Alloc>::const_iterator
cf1e0371 1961 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
151fbaac 1962 _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
dc4871cb 1963 const _Key& __k) const
cf1e0371 1964 {
cf1e0371 1965 while (__x != 0)
dc4871cb
PC
1966 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1967 __y = __x, __x = _S_left(__x);
1968 else
1969 __x = _S_right(__x);
f7e52577 1970 return const_iterator(__y);
dc4871cb
PC
1971 }
1972
1973 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1974 typename _Compare, typename _Alloc>
f7e52577
PC
1975 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1976 _Compare, _Alloc>::iterator
1977 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
151fbaac 1978 _M_upper_bound(_Link_type __x, _Base_ptr __y,
f7e52577
PC
1979 const _Key& __k)
1980 {
1981 while (__x != 0)
1982 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1983 __y = __x, __x = _S_left(__x);
1984 else
1985 __x = _S_right(__x);
1986 return iterator(__y);
1987 }
1988
1989 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 1990 typename _Compare, typename _Alloc>
f7e52577
PC
1991 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1992 _Compare, _Alloc>::const_iterator
dc4871cb 1993 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
151fbaac 1994 _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
dc4871cb
PC
1995 const _Key& __k) const
1996 {
1997 while (__x != 0)
1998 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1999 __y = __x, __x = _S_left(__x);
2000 else
2001 __x = _S_right(__x);
f7e52577 2002 return const_iterator(__y);
cf1e0371
PC
2003 }
2004
639b490b 2005 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2006 typename _Compare, typename _Alloc>
639b490b
PC
2007 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2008 _Compare, _Alloc>::iterator,
f7e52577
PC
2009 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2010 _Compare, _Alloc>::iterator>
2011 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2012 equal_range(const _Key& __k)
2013 {
2014 _Link_type __x = _M_begin();
151fbaac 2015 _Base_ptr __y = _M_end();
f7e52577
PC
2016 while (__x != 0)
2017 {
2018 if (_M_impl._M_key_compare(_S_key(__x), __k))
2019 __x = _S_right(__x);
2020 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
2021 __y = __x, __x = _S_left(__x);
2022 else
2023 {
151fbaac
JW
2024 _Link_type __xu(__x);
2025 _Base_ptr __yu(__y);
f7e52577
PC
2026 __y = __x, __x = _S_left(__x);
2027 __xu = _S_right(__xu);
2028 return pair<iterator,
fee84d55 2029 iterator>(_M_lower_bound(__x, __y, __k),
f7e52577
PC
2030 _M_upper_bound(__xu, __yu, __k));
2031 }
2032 }
2033 return pair<iterator, iterator>(iterator(__y),
2034 iterator(__y));
2035 }
2036
2037 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2038 typename _Compare, typename _Alloc>
f7e52577
PC
2039 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2040 _Compare, _Alloc>::const_iterator,
2041 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2042 _Compare, _Alloc>::const_iterator>
639b490b 2043 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
f7e52577 2044 equal_range(const _Key& __k) const
639b490b
PC
2045 {
2046 _Const_Link_type __x = _M_begin();
151fbaac 2047 _Const_Base_ptr __y = _M_end();
639b490b
PC
2048 while (__x != 0)
2049 {
2050 if (_M_impl._M_key_compare(_S_key(__x), __k))
2051 __x = _S_right(__x);
2052 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
2053 __y = __x, __x = _S_left(__x);
2054 else
2055 {
151fbaac
JW
2056 _Const_Link_type __xu(__x);
2057 _Const_Base_ptr __yu(__y);
639b490b
PC
2058 __y = __x, __x = _S_left(__x);
2059 __xu = _S_right(__xu);
f7e52577 2060 return pair<const_iterator,
fee84d55 2061 const_iterator>(_M_lower_bound(__x, __y, __k),
f7e52577 2062 _M_upper_bound(__xu, __yu, __k));
639b490b
PC
2063 }
2064 }
f7e52577
PC
2065 return pair<const_iterator, const_iterator>(const_iterator(__y),
2066 const_iterator(__y));
639b490b
PC
2067 }
2068
ed6814f7 2069 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2070 typename _Compare, typename _Alloc>
7f6dd1ca 2071 void
11aaf40c 2072 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
c5d9ec56
JW
2073 swap(_Rb_tree& __t)
2074 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
7f6dd1ca
GB
2075 {
2076 if (_M_root() == 0)
7f6dd1ca 2077 {
42a27024 2078 if (__t._M_root() != 0)
a4dec0d6 2079 _M_impl._M_move_data(__t._M_impl);
7f6dd1ca 2080 }
7f6dd1ca 2081 else if (__t._M_root() == 0)
a4dec0d6 2082 __t._M_impl._M_move_data(_M_impl);
7f6dd1ca 2083 else
42a27024
PC
2084 {
2085 std::swap(_M_root(),__t._M_root());
2086 std::swap(_M_leftmost(),__t._M_leftmost());
2087 std::swap(_M_rightmost(),__t._M_rightmost());
fee84d55 2088
42a27024
PC
2089 _M_root()->_M_parent = _M_end();
2090 __t._M_root()->_M_parent = __t._M_end();
c6195f58 2091 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
42a27024 2092 }
7f6dd1ca 2093 // No need to swap header's color as it does not change.
8bd22a3c 2094 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
ff90a89e
JW
2095
2096 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2097 __t._M_get_Node_allocator());
7f6dd1ca
GB
2098 }
2099
ed6814f7 2100 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2101 typename _Compare, typename _Alloc>
11aaf40c 2102 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
55826ab6
FD
2103 _Compare, _Alloc>::_Base_ptr,
2104 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2105 _Compare, _Alloc>::_Base_ptr>
11aaf40c 2106 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
55826ab6 2107 _M_get_insert_unique_pos(const key_type& __k)
d3d526ac 2108 {
55826ab6 2109 typedef pair<_Base_ptr, _Base_ptr> _Res;
b4c70e89 2110 _Link_type __x = _M_begin();
151fbaac 2111 _Base_ptr __y = _M_end();
d3d526ac 2112 bool __comp = true;
62e67651 2113 while (__x != 0)
d3d526ac
BK
2114 {
2115 __y = __x;
55826ab6 2116 __comp = _M_impl._M_key_compare(__k, _S_key(__x));
d3d526ac
BK
2117 __x = __comp ? _S_left(__x) : _S_right(__x);
2118 }
62e67651 2119 iterator __j = iterator(__y);
d3d526ac 2120 if (__comp)
2a67bec2
ILT
2121 {
2122 if (__j == begin())
55826ab6 2123 return _Res(__x, __y);
2a67bec2
ILT
2124 else
2125 --__j;
2126 }
55826ab6
FD
2127 if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
2128 return _Res(__x, __y);
2129 return _Res(__j._M_node, 0);
d3d526ac 2130 }
ed6814f7
BI
2131
2132 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2133 typename _Compare, typename _Alloc>
55826ab6
FD
2134 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2135 _Compare, _Alloc>::_Base_ptr,
2136 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2137 _Compare, _Alloc>::_Base_ptr>
d3d526ac 2138 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
55826ab6 2139 _M_get_insert_equal_pos(const key_type& __k)
d3d526ac 2140 {
55826ab6 2141 typedef pair<_Base_ptr, _Base_ptr> _Res;
dc4871cb 2142 _Link_type __x = _M_begin();
151fbaac 2143 _Base_ptr __y = _M_end();
dc4871cb 2144 while (__x != 0)
ec5537cc 2145 {
dc4871cb 2146 __y = __x;
55826ab6 2147 __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
fee84d55 2148 _S_left(__x) : _S_right(__x);
d3d526ac 2149 }
55826ab6
FD
2150 return _Res(__x, __y);
2151 }
2152
2153 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2154 typename _Compare, typename _Alloc>
734f5023 2155#if __cplusplus >= 201103L
55826ab6
FD
2156 template<typename _Arg>
2157#endif
2158 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2159 _Compare, _Alloc>::iterator, bool>
2160 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
734f5023 2161#if __cplusplus >= 201103L
55826ab6
FD
2162 _M_insert_unique(_Arg&& __v)
2163#else
2164 _M_insert_unique(const _Val& __v)
2165#endif
2166 {
2167 typedef pair<iterator, bool> _Res;
2168 pair<_Base_ptr, _Base_ptr> __res
2169 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
2170
2171 if (__res.second)
c6195f58
FD
2172 {
2173 _Alloc_node __an(*this);
2174 return _Res(_M_insert_(__res.first, __res.second,
2175 _GLIBCXX_FORWARD(_Arg, __v), __an),
2176 true);
2177 }
55826ab6 2178
151fbaac 2179 return _Res(iterator(__res.first), false);
725dc051 2180 }
d3d526ac 2181
d5e07b79 2182 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2183 typename _Compare, typename _Alloc>
734f5023 2184#if __cplusplus >= 201103L
e6a05448
PC
2185 template<typename _Arg>
2186#endif
dc4871cb 2187 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
d5e07b79 2188 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
734f5023 2189#if __cplusplus >= 201103L
55826ab6 2190 _M_insert_equal(_Arg&& __v)
e6a05448 2191#else
55826ab6 2192 _M_insert_equal(const _Val& __v)
e6a05448 2193#endif
d5e07b79 2194 {
55826ab6
FD
2195 pair<_Base_ptr, _Base_ptr> __res
2196 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
c6195f58
FD
2197 _Alloc_node __an(*this);
2198 return _M_insert_(__res.first, __res.second,
2199 _GLIBCXX_FORWARD(_Arg, __v), __an);
55826ab6
FD
2200 }
2201
2202 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2203 typename _Compare, typename _Alloc>
55826ab6
FD
2204 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2205 _Compare, _Alloc>::_Base_ptr,
fee84d55 2206 typename _Rb_tree<_Key, _Val, _KeyOfValue,
55826ab6
FD
2207 _Compare, _Alloc>::_Base_ptr>
2208 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2209 _M_get_insert_hint_unique_pos(const_iterator __position,
2210 const key_type& __k)
2211 {
2212 iterator __pos = __position._M_const_cast();
2213 typedef pair<_Base_ptr, _Base_ptr> _Res;
2214
d5e07b79 2215 // end()
55826ab6 2216 if (__pos._M_node == _M_end())
d5e07b79
PC
2217 {
2218 if (size() > 0
55826ab6
FD
2219 && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
2220 return _Res(0, _M_rightmost());
d5e07b79 2221 else
55826ab6 2222 return _M_get_insert_unique_pos(__k);
d5e07b79 2223 }
55826ab6 2224 else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
d5e07b79
PC
2225 {
2226 // First, try before...
55826ab6
FD
2227 iterator __before = __pos;
2228 if (__pos._M_node == _M_leftmost()) // begin()
2229 return _Res(_M_leftmost(), _M_leftmost());
2230 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
d5e07b79
PC
2231 {
2232 if (_S_right(__before._M_node) == 0)
55826ab6 2233 return _Res(0, __before._M_node);
d5e07b79 2234 else
55826ab6 2235 return _Res(__pos._M_node, __pos._M_node);
d5e07b79
PC
2236 }
2237 else
55826ab6 2238 return _M_get_insert_unique_pos(__k);
d5e07b79 2239 }
55826ab6 2240 else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
d5e07b79
PC
2241 {
2242 // ... then try after.
55826ab6
FD
2243 iterator __after = __pos;
2244 if (__pos._M_node == _M_rightmost())
2245 return _Res(0, _M_rightmost());
2246 else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
d5e07b79 2247 {
55826ab6
FD
2248 if (_S_right(__pos._M_node) == 0)
2249 return _Res(0, __pos._M_node);
d5e07b79 2250 else
55826ab6 2251 return _Res(__after._M_node, __after._M_node);
d5e07b79
PC
2252 }
2253 else
55826ab6 2254 return _M_get_insert_unique_pos(__k);
d5e07b79
PC
2255 }
2256 else
dc4871cb 2257 // Equivalent keys.
55826ab6 2258 return _Res(__pos._M_node, 0);
d5e07b79
PC
2259 }
2260
ed6814f7 2261 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2262 typename _Compare, typename _Alloc>
734f5023 2263#if __cplusplus >= 201103L
c6195f58
FD
2264 template<typename _Arg, typename _NodeGen>
2265#else
2266 template<typename _NodeGen>
e6a05448 2267#endif
c6195f58
FD
2268 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2269 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2270 _M_insert_unique_(const_iterator __position,
734f5023 2271#if __cplusplus >= 201103L
c6195f58 2272 _Arg&& __v,
e6a05448 2273#else
c6195f58 2274 const _Val& __v,
e6a05448 2275#endif
c6195f58 2276 _NodeGen& __node_gen)
d3d526ac 2277 {
55826ab6
FD
2278 pair<_Base_ptr, _Base_ptr> __res
2279 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
2280
2281 if (__res.second)
2282 return _M_insert_(__res.first, __res.second,
c6195f58
FD
2283 _GLIBCXX_FORWARD(_Arg, __v),
2284 __node_gen);
151fbaac 2285 return iterator(__res.first);
55826ab6
FD
2286 }
2287
2288 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2289 typename _Compare, typename _Alloc>
55826ab6
FD
2290 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2291 _Compare, _Alloc>::_Base_ptr,
fee84d55 2292 typename _Rb_tree<_Key, _Val, _KeyOfValue,
55826ab6
FD
2293 _Compare, _Alloc>::_Base_ptr>
2294 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2295 _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
2296 {
2297 iterator __pos = __position._M_const_cast();
2298 typedef pair<_Base_ptr, _Base_ptr> _Res;
2299
ec5537cc 2300 // end()
55826ab6 2301 if (__pos._M_node == _M_end())
ed6814f7 2302 {
62e67651 2303 if (size() > 0
55826ab6
FD
2304 && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
2305 return _Res(0, _M_rightmost());
d3d526ac 2306 else
55826ab6 2307 return _M_get_insert_equal_pos(__k);
ed6814f7 2308 }
55826ab6 2309 else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
d5e07b79
PC
2310 {
2311 // First, try before...
55826ab6
FD
2312 iterator __before = __pos;
2313 if (__pos._M_node == _M_leftmost()) // begin()
2314 return _Res(_M_leftmost(), _M_leftmost());
2315 else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
d5e07b79
PC
2316 {
2317 if (_S_right(__before._M_node) == 0)
55826ab6 2318 return _Res(0, __before._M_node);
d5e07b79 2319 else
55826ab6 2320 return _Res(__pos._M_node, __pos._M_node);
d5e07b79
PC
2321 }
2322 else
55826ab6 2323 return _M_get_insert_equal_pos(__k);
d5e07b79
PC
2324 }
2325 else
2326 {
fee84d55 2327 // ... then try after.
55826ab6
FD
2328 iterator __after = __pos;
2329 if (__pos._M_node == _M_rightmost())
2330 return _Res(0, _M_rightmost());
2331 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
d5e07b79 2332 {
55826ab6
FD
2333 if (_S_right(__pos._M_node) == 0)
2334 return _Res(0, __pos._M_node);
d5e07b79 2335 else
55826ab6 2336 return _Res(__after._M_node, __after._M_node);
d5e07b79
PC
2337 }
2338 else
55826ab6 2339 return _Res(0, 0);
d5e07b79
PC
2340 }
2341 }
2342
55826ab6 2343 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2344 typename _Compare, typename _Alloc>
734f5023 2345#if __cplusplus >= 201103L
c6195f58
FD
2346 template<typename _Arg, typename _NodeGen>
2347#else
2348 template<typename _NodeGen>
55826ab6 2349#endif
c6195f58
FD
2350 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2351 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2352 _M_insert_equal_(const_iterator __position,
734f5023 2353#if __cplusplus >= 201103L
c6195f58 2354 _Arg&& __v,
55826ab6 2355#else
c6195f58 2356 const _Val& __v,
55826ab6 2357#endif
c6195f58
FD
2358 _NodeGen& __node_gen)
2359 {
2360 pair<_Base_ptr, _Base_ptr> __res
2361 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
55826ab6 2362
c6195f58
FD
2363 if (__res.second)
2364 return _M_insert_(__res.first, __res.second,
2365 _GLIBCXX_FORWARD(_Arg, __v),
2366 __node_gen);
55826ab6 2367
c6195f58
FD
2368 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
2369 }
55826ab6 2370
734f5023 2371#if __cplusplus >= 201103L
55826ab6 2372 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2373 typename _Compare, typename _Alloc>
bf548ce3 2374 auto
55826ab6
FD
2375 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2376 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
bf548ce3 2377 -> iterator
55826ab6
FD
2378 {
2379 bool __insert_left = (__x != 0 || __p == _M_end()
2380 || _M_impl._M_key_compare(_S_key(__z),
2381 _S_key(__p)));
2382
2383 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2384 this->_M_impl._M_header);
2385 ++_M_impl._M_node_count;
2386 return iterator(__z);
2387 }
2388
2389 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2390 typename _Compare, typename _Alloc>
bf548ce3 2391 auto
55826ab6
FD
2392 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2393 _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
bf548ce3 2394 -> iterator
55826ab6
FD
2395 {
2396 bool __insert_left = (__p == _M_end()
2397 || !_M_impl._M_key_compare(_S_key(__p),
2398 _S_key(__z)));
2399
2400 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2401 this->_M_impl._M_header);
2402 ++_M_impl._M_node_count;
2403 return iterator(__z);
2404 }
2405
2406 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2407 typename _Compare, typename _Alloc>
bf548ce3 2408 auto
55826ab6
FD
2409 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2410 _M_insert_equal_lower_node(_Link_type __z)
bf548ce3 2411 -> iterator
55826ab6
FD
2412 {
2413 _Link_type __x = _M_begin();
151fbaac 2414 _Base_ptr __y = _M_end();
55826ab6
FD
2415 while (__x != 0)
2416 {
2417 __y = __x;
2418 __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
fee84d55 2419 _S_left(__x) : _S_right(__x);
55826ab6
FD
2420 }
2421 return _M_insert_lower_node(__y, __z);
2422 }
2423
2424 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2425 typename _Compare, typename _Alloc>
55826ab6 2426 template<typename... _Args>
bf548ce3 2427 auto
55826ab6
FD
2428 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2429 _M_emplace_unique(_Args&&... __args)
bf548ce3 2430 -> pair<iterator, bool>
55826ab6 2431 {
bf548ce3
JW
2432 _Auto_node __z(*this, std::forward<_Args>(__args)...);
2433 auto __res = _M_get_insert_unique_pos(__z._M_key());
2434 if (__res.second)
2435 return {__z._M_insert(__res), true};
2436 return {iterator(__res.first), false};
55826ab6
FD
2437 }
2438
2439 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2440 typename _Compare, typename _Alloc>
55826ab6 2441 template<typename... _Args>
bf548ce3 2442 auto
55826ab6
FD
2443 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2444 _M_emplace_equal(_Args&&... __args)
bf548ce3 2445 -> iterator
55826ab6 2446 {
bf548ce3
JW
2447 _Auto_node __z(*this, std::forward<_Args>(__args)...);
2448 auto __res = _M_get_insert_equal_pos(__z._M_key());
2449 return __z._M_insert(__res);
55826ab6
FD
2450 }
2451
2452 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2453 typename _Compare, typename _Alloc>
55826ab6 2454 template<typename... _Args>
bf548ce3 2455 auto
55826ab6
FD
2456 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2457 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
bf548ce3 2458 -> iterator
55826ab6 2459 {
bf548ce3
JW
2460 _Auto_node __z(*this, std::forward<_Args>(__args)...);
2461 auto __res = _M_get_insert_hint_unique_pos(__pos, __z._M_key());
2462 if (__res.second)
2463 return __z._M_insert(__res);
2464 return iterator(__res.first);
55826ab6
FD
2465 }
2466
2467 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2468 typename _Compare, typename _Alloc>
55826ab6 2469 template<typename... _Args>
bf548ce3 2470 auto
55826ab6
FD
2471 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2472 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
bf548ce3 2473 -> iterator
55826ab6 2474 {
bf548ce3
JW
2475 _Auto_node __z(*this, std::forward<_Args>(__args)...);
2476 auto __res = _M_get_insert_hint_equal_pos(__pos, __z._M_key());
2477 if (__res.second)
2478 return __z._M_insert(__res);
2479 return __z._M_insert_equal_lower();
55826ab6
FD
2480 }
2481#endif
2482
725dc051 2483
c105751c 2484 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2485 typename _Compare, typename _Alloc>
7606bd11 2486 void
d5e07b79 2487 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
7606bd11 2488 _M_erase_aux(const_iterator __position)
d5e07b79
PC
2489 {
2490 _Link_type __y =
2491 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
2492 (const_cast<_Base_ptr>(__position._M_node),
2493 this->_M_impl._M_header));
c6195f58 2494 _M_drop_node(__y);
d5e07b79
PC
2495 --_M_impl._M_node_count;
2496 }
c105751c 2497
ed6814f7 2498 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2499 typename _Compare, typename _Alloc>
ed6814f7 2500 void
11aaf40c 2501 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
7606bd11 2502 _M_erase_aux(const_iterator __first, const_iterator __last)
d3d526ac
BK
2503 {
2504 if (__first == begin() && __last == end())
2505 clear();
2506 else
a3186d4e 2507 while (__first != __last)
36b88526 2508 _M_erase_aux(__first++);
725dc051 2509 }
d3d526ac 2510
d5e07b79 2511 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2512 typename _Compare, typename _Alloc>
7606bd11 2513 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
d5e07b79 2514 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
7606bd11 2515 erase(const _Key& __x)
d5e07b79 2516 {
7606bd11
PC
2517 pair<iterator, iterator> __p = equal_range(__x);
2518 const size_type __old_size = size();
36b88526 2519 _M_erase_aux(__p.first, __p.second);
7606bd11 2520 return __old_size - size();
d5e07b79
PC
2521 }
2522
ed6814f7 2523 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2524 typename _Compare, typename _Alloc>
f7e52577
PC
2525 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2526 _Compare, _Alloc>::iterator
11aaf40c
PC
2527 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2528 find(const _Key& __k)
d3d526ac 2529 {
f7e52577 2530 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
11aaf40c
PC
2531 return (__j == end()
2532 || _M_impl._M_key_compare(__k,
2533 _S_key(__j._M_node))) ? end() : __j;
d3d526ac 2534 }
ed6814f7
BI
2535
2536 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2537 typename _Compare, typename _Alloc>
f7e52577
PC
2538 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2539 _Compare, _Alloc>::const_iterator
11aaf40c 2540 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
d3d526ac
BK
2541 find(const _Key& __k) const
2542 {
f7e52577 2543 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
dc4871cb 2544 return (__j == end()
fee84d55 2545 || _M_impl._M_key_compare(__k,
dc4871cb 2546 _S_key(__j._M_node))) ? end() : __j;
725dc051 2547 }
d3d526ac 2548
ed6814f7 2549 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2550 typename _Compare, typename _Alloc>
11aaf40c
PC
2551 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
2552 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
d3d526ac 2553 count(const _Key& __k) const
322821b9 2554 {
d3d526ac 2555 pair<const_iterator, const_iterator> __p = equal_range(__k);
62e67651 2556 const size_type __n = std::distance(__p.first, __p.second);
d3d526ac 2557 return __n;
322821b9 2558 }
d3d526ac 2559
b8add594 2560 _GLIBCXX_PURE unsigned int
b4c70e89 2561 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
fee84d55 2562 const _Rb_tree_node_base* __root) throw ();
d3d526ac 2563
ed6814f7 2564 template<typename _Key, typename _Val, typename _KeyOfValue,
fee84d55 2565 typename _Compare, typename _Alloc>
ed6814f7 2566 bool
d3d526ac
BK
2567 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
2568 {
8bd22a3c
BK
2569 if (_M_impl._M_node_count == 0 || begin() == end())
2570 return _M_impl._M_node_count == 0 && begin() == end()
2571 && this->_M_impl._M_header._M_left == _M_end()
2572 && this->_M_impl._M_header._M_right == _M_end();
ed6814f7 2573
737ab798 2574 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
ed6814f7 2575 for (const_iterator __it = begin(); __it != end(); ++__it)
737ab798
PC
2576 {
2577 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
2578 _Const_Link_type __L = _S_left(__x);
2579 _Const_Link_type __R = _S_right(__x);
ed6814f7 2580
737ab798 2581 if (__x->_M_color == _S_red)
ed6814f7 2582 if ((__L && __L->_M_color == _S_red)
737ab798
PC
2583 || (__R && __R->_M_color == _S_red))
2584 return false;
ed6814f7 2585
8bd22a3c 2586 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
737ab798 2587 return false;
8bd22a3c 2588 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
737ab798 2589 return false;
ed6814f7 2590
737ab798
PC
2591 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
2592 return false;
2593 }
ed6814f7 2594
737ab798
PC
2595 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
2596 return false;
2597 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
2598 return false;
2599 return true;
d3d526ac 2600 }
3cbc7af0 2601
2dbe56bd
JW
2602#if __cplusplus > 201402L
2603 // Allow access to internals of compatible _Rb_tree specializations.
2604 template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
2605 typename _Alloc, typename _Cmp2>
2606 struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
2607 _Cmp2>
2608 {
2609 private:
2610 friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
2611
2612 static auto&
2613 _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
2614 { return __tree._M_impl; }
2615 };
2616#endif // C++17
2617
12ffa228
BK
2618_GLIBCXX_END_NAMESPACE_VERSION
2619} // namespace
725dc051 2620
ed6814f7 2621#endif