libstdc++
stl_tree.h
Go to the documentation of this file.
1// RB tree implementation -*- C++ -*-
2
3// Copyright (C) 2001-2025 Free Software Foundation, Inc.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
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
53/** @file bits/stl_tree.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{map,set}
56 */
57
58#ifndef _STL_TREE_H
59#define _STL_TREE_H 1
60
61#ifdef _GLIBCXX_SYSHDR
62#pragma GCC system_header
63#endif
64
65#include <bits/stl_algobase.h>
66#include <bits/allocator.h>
67#include <bits/stl_function.h>
69#include <bits/ptr_traits.h>
70#include <ext/alloc_traits.h>
71#if __cplusplus >= 201103L
72# include <ext/aligned_buffer.h>
73#endif
74#ifdef __glibcxx_node_extract // >= C++17
75# include <bits/node_handle.h>
76#endif
77
78#if __cplusplus < 201103L
79# undef _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
80# define _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE 0
81#elif ! defined _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
82# define _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE 1
83#endif
84
85namespace std _GLIBCXX_VISIBILITY(default)
86{
87_GLIBCXX_BEGIN_NAMESPACE_VERSION
88
89 // Red-black tree class, designed for use in implementing STL
90 // associative containers (set, multiset, map, and multimap). The
91 // insertion and deletion algorithms are based on those in Cormen,
92 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
93 // 1990), except that
94 //
95 // (1) the header cell is maintained with links not only to the root
96 // but also to the leftmost node of the tree, to enable constant
97 // time begin(), and to the rightmost node of the tree, to enable
98 // linear time performance when used with the generic set algorithms
99 // (set_union, etc.)
100 //
101 // (2) when a node being deleted has two children its successor node
102 // is relinked into its place, rather than copied, so that the only
103 // iterators invalidated are those referring to the deleted node.
104
105 enum _Rb_tree_color { _S_red = false, _S_black = true };
106
107 struct _Rb_tree_node_base
108 {
109 typedef _Rb_tree_node_base* _Base_ptr;
110
111 _Rb_tree_color _M_color;
112 _Base_ptr _M_parent;
113 _Base_ptr _M_left;
114 _Base_ptr _M_right;
115
116 static _Base_ptr
117 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
118 {
119 while (__x->_M_left != 0) __x = __x->_M_left;
120 return __x;
121 }
122
123 static _Base_ptr
124 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
125 {
126 while (__x->_M_right != 0) __x = __x->_M_right;
127 return __x;
128 }
129
130 // This is not const-correct, but it's only used in a const access path
131 // by std::_Rb_tree::_M_end() where the pointer is used to initialize a
132 // const_iterator and so constness is restored.
133 _Base_ptr
134 _M_base_ptr() const _GLIBCXX_NOEXCEPT
135 { return const_cast<_Rb_tree_node_base*>(this); }
136 };
137
138 // Helper type offering value initialization guarantee on the compare functor.
139 template<typename _Key_compare>
140 struct _Rb_tree_key_compare
141 {
142 _Key_compare _M_key_compare;
143
144 _Rb_tree_key_compare()
145 _GLIBCXX_NOEXCEPT_IF(
146 is_nothrow_default_constructible<_Key_compare>::value)
147 : _M_key_compare()
148 { }
149
150 _Rb_tree_key_compare(const _Key_compare& __comp)
151 : _M_key_compare(__comp)
152 { }
153
154#if __cplusplus >= 201103L
155 // Copy constructor added for consistency with C++98 mode.
156 _Rb_tree_key_compare(const _Rb_tree_key_compare&) = default;
157
158 _Rb_tree_key_compare(_Rb_tree_key_compare&& __x)
159 noexcept(is_nothrow_copy_constructible<_Key_compare>::value)
160 : _M_key_compare(__x._M_key_compare)
161 { }
162#endif
163 };
164
165 // Helper type to manage default initialization of node count and header.
166 struct _Rb_tree_header
167 {
168 _Rb_tree_node_base _M_header;
169 size_t _M_node_count; // Keeps track of size of tree.
170
171 _Rb_tree_header() _GLIBCXX_NOEXCEPT
172 {
173 _M_header._M_color = _S_red;
174 _M_reset();
175 }
176
177#if __cplusplus >= 201103L
178 _Rb_tree_header(_Rb_tree_header&& __x) noexcept
179 {
180 if (__x._M_header._M_parent != nullptr)
181 _M_move_data(__x);
182 else
183 {
184 _M_header._M_color = _S_red;
185 _M_reset();
186 }
187 }
188#endif
189
190 void
191 _M_move_data(_Rb_tree_header& __from)
192 {
193 _M_header._M_color = __from._M_header._M_color;
194 _M_header._M_parent = __from._M_header._M_parent;
195 _M_header._M_left = __from._M_header._M_left;
196 _M_header._M_right = __from._M_header._M_right;
197 _M_header._M_parent->_M_parent = &_M_header;
198 _M_node_count = __from._M_node_count;
199
200 __from._M_reset();
201 }
202
203 void
204 _M_reset()
205 {
206 _M_header._M_parent = 0;
207 _M_header._M_left = &_M_header;
208 _M_header._M_right = &_M_header;
209 _M_node_count = 0;
210 }
211 };
212
213 template<typename _Val>
214 struct _Rb_tree_node : public _Rb_tree_node_base
215 {
216#if __cplusplus < 201103L
217 _Val _M_value_field;
218
219 _Val*
220 _M_valptr()
221 { return std::__addressof(_M_value_field); }
222
223 const _Val*
224 _M_valptr() const
225 { return std::__addressof(_M_value_field); }
226#else
227 __gnu_cxx::__aligned_membuf<_Val> _M_storage;
228
229 _Val*
230 _M_valptr()
231 { return _M_storage._M_ptr(); }
232
233 const _Val*
234 _M_valptr() const
235 { return _M_storage._M_ptr(); }
236#endif
237
238 _Rb_tree_node*
239 _M_node_ptr() _GLIBCXX_NOEXCEPT
240 { return this; }
241 };
242
243#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
244namespace __rb_tree
245{
246 template<typename _VoidPtr>
247 struct _Node_base
248 {
249 using _Base_ptr = __ptr_rebind<_VoidPtr, _Node_base>;
250
251 _Rb_tree_color _M_color;
252 _Base_ptr _M_parent;
253 _Base_ptr _M_left;
254 _Base_ptr _M_right;
255
256 static _Base_ptr
257 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
258 {
259 while (__x->_M_left) __x = __x->_M_left;
260 return __x;
261 }
262
263 static _Base_ptr
264 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
265 {
266 while (__x->_M_right) __x = __x->_M_right;
267 return __x;
268 }
269
270 // This is not const-correct, but it's only used in a const access path
271 // by std::_Rb_tree::_M_end() where the pointer is used to initialize a
272 // const_iterator and so constness is restored.
273 _Base_ptr
274 _M_base_ptr() const noexcept
275 {
276 return pointer_traits<_Base_ptr>::pointer_to
277 (*const_cast<_Node_base*>(this));
278 }
279 };
280
281 // Helper type to manage default initialization of node count and header.
282 template<typename _NodeBase>
283 struct _Header
284 {
285 private:
286 using _Base_ptr = typename _NodeBase::_Base_ptr;
287
288 public:
289 _NodeBase _M_header;
290 size_t _M_node_count; // Keeps track of size of tree.
291
292 _Header() noexcept
293 {
294 _M_header._M_color = _S_red;
295 _M_reset();
296 }
297
298 _Header(_Header&& __x) noexcept
299 {
300 if (__x._M_header._M_parent)
301 _M_move_data(__x);
302 else
303 {
304 _M_header._M_color = _S_red;
305 _M_reset();
306 }
307 }
308
309 void
310 _M_move_data(_Header& __from)
311 {
312 _M_header._M_color = __from._M_header._M_color;
313 _M_header._M_parent = __from._M_header._M_parent;
314 _M_header._M_left = __from._M_header._M_left;
315 _M_header._M_right = __from._M_header._M_right;
316 _M_header._M_parent->_M_parent = _M_header._M_base_ptr();
317 _M_node_count = __from._M_node_count;
318
319 __from._M_reset();
320 }
321
322 void
323 _M_reset()
324 {
325 _M_header._M_parent = nullptr;
326 _M_header._M_left = _M_header._M_right = _M_header._M_base_ptr();
327 _M_node_count = 0;
328 }
329 };
330
331 template<typename _ValPtr>
332 struct _Node : public __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>
333 {
334 using value_type = typename pointer_traits<_ValPtr>::element_type;
335 using _Node_ptr = __ptr_rebind<_ValPtr, _Node>;
336
337 _Node() noexcept { }
338 ~_Node() { }
339 _Node(_Node&&) = delete;
340
341 union _Uninit_storage
342 {
343 _Uninit_storage() noexcept { }
344 ~_Uninit_storage() { }
345
346 value_type _M_data;
347 };
348 _Uninit_storage _M_u;
349
350 value_type*
351 _M_valptr()
352 { return std::addressof(_M_u._M_data); }
353
354 value_type const*
355 _M_valptr() const
356 { return std::addressof(_M_u._M_data); }
357
358 _Node_ptr
359 _M_node_ptr() noexcept
360 { return pointer_traits<_Node_ptr>::pointer_to(*this); }
361 };
362} // namespace __rb_tree
363#endif // _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
364
365 _GLIBCXX_PURE _Rb_tree_node_base*
366 _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
367
368 _GLIBCXX_PURE _Rb_tree_node_base*
369 _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
370
371 template<typename _Tp>
372 struct _Rb_tree_iterator
373 {
374 typedef _Tp value_type;
375 typedef _Tp& reference;
376 typedef _Tp* pointer;
377
378 typedef bidirectional_iterator_tag iterator_category;
379 typedef ptrdiff_t difference_type;
380
381 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
382 typedef _Rb_tree_node<_Tp>* _Node_ptr;
383
384 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
385 : _M_node() { }
386
387 explicit
388 _Rb_tree_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
389 : _M_node(__x) { }
390
391 reference
392 operator*() const _GLIBCXX_NOEXCEPT
393 { return *static_cast<_Node_ptr>(_M_node)->_M_valptr(); }
394
395 pointer
396 operator->() const _GLIBCXX_NOEXCEPT
397 { return static_cast<_Node_ptr>(_M_node)->_M_valptr(); }
398
399 _Rb_tree_iterator&
400 operator++() _GLIBCXX_NOEXCEPT
401 {
402 _M_node = _Rb_tree_increment(_M_node);
403 return *this;
404 }
405
406 _Rb_tree_iterator
407 operator++(int) _GLIBCXX_NOEXCEPT
408 {
409 _Rb_tree_iterator __tmp = *this;
410 _M_node = _Rb_tree_increment(_M_node);
411 return __tmp;
412 }
413
414 _Rb_tree_iterator&
415 operator--() _GLIBCXX_NOEXCEPT
416 {
417 _M_node = _Rb_tree_decrement(_M_node);
418 return *this;
419 }
420
421 _Rb_tree_iterator
422 operator--(int) _GLIBCXX_NOEXCEPT
423 {
424 _Rb_tree_iterator __tmp = *this;
425 _M_node = _Rb_tree_decrement(_M_node);
426 return __tmp;
427 }
428
429 friend bool
430 operator==(const _Rb_tree_iterator& __x,
431 const _Rb_tree_iterator& __y) _GLIBCXX_NOEXCEPT
432 { return __x._M_node == __y._M_node; }
433
434#if ! __cpp_lib_three_way_comparison
435 friend bool
436 operator!=(const _Rb_tree_iterator& __x,
437 const _Rb_tree_iterator& __y) _GLIBCXX_NOEXCEPT
438 { return __x._M_node != __y._M_node; }
439#endif
440
441 _Base_ptr _M_node;
442 };
443
444 template<typename _Tp>
445 struct _Rb_tree_const_iterator
446 {
447 typedef _Tp value_type;
448 typedef const _Tp& reference;
449 typedef const _Tp* pointer;
450
451 typedef _Rb_tree_iterator<_Tp> iterator;
452
453 typedef bidirectional_iterator_tag iterator_category;
454 typedef ptrdiff_t difference_type;
455
456 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
457 typedef const _Rb_tree_node<_Tp>* _Node_ptr;
458
459 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
460 : _M_node() { }
461
462 explicit
463 _Rb_tree_const_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
464 : _M_node(__x) { }
465
466 _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPT
467 : _M_node(__it._M_node) { }
468
469 reference
470 operator*() const _GLIBCXX_NOEXCEPT
471 { return *static_cast<_Node_ptr>(_M_node)->_M_valptr(); }
472
473 pointer
474 operator->() const _GLIBCXX_NOEXCEPT
475 { return static_cast<_Node_ptr>(_M_node)->_M_valptr(); }
476
477 _Rb_tree_const_iterator&
478 operator++() _GLIBCXX_NOEXCEPT
479 {
480 _M_node = _Rb_tree_increment(_M_node);
481 return *this;
482 }
483
484 _Rb_tree_const_iterator
485 operator++(int) _GLIBCXX_NOEXCEPT
486 {
487 _Rb_tree_const_iterator __tmp = *this;
488 _M_node = _Rb_tree_increment(_M_node);
489 return __tmp;
490 }
491
492 _Rb_tree_const_iterator&
493 operator--() _GLIBCXX_NOEXCEPT
494 {
495 _M_node = _Rb_tree_decrement(_M_node);
496 return *this;
497 }
498
499 _Rb_tree_const_iterator
500 operator--(int) _GLIBCXX_NOEXCEPT
501 {
502 _Rb_tree_const_iterator __tmp = *this;
503 _M_node = _Rb_tree_decrement(_M_node);
504 return __tmp;
505 }
506
507 friend bool
508 operator==(const _Rb_tree_const_iterator& __x,
509 const _Rb_tree_const_iterator& __y) _GLIBCXX_NOEXCEPT
510 { return __x._M_node == __y._M_node; }
511
512#if ! __cpp_lib_three_way_comparison
513 friend bool
514 operator!=(const _Rb_tree_const_iterator& __x,
515 const _Rb_tree_const_iterator& __y) _GLIBCXX_NOEXCEPT
516 { return __x._M_node != __y._M_node; }
517#endif
518
519 _Base_ptr _M_node;
520 };
521
522 __attribute__((__nonnull__))
523 void
524 _Rb_tree_insert_and_rebalance(const bool __insert_left,
525 _Rb_tree_node_base* __x,
526 _Rb_tree_node_base* __p,
527 _Rb_tree_node_base& __header) throw ();
528
529 __attribute__((__nonnull__,__returns_nonnull__))
530 _Rb_tree_node_base*
531 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
532 _Rb_tree_node_base& __header) throw ();
533
534namespace __rb_tree
535{
536#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
537 template<bool _Const, typename _ValPtr>
538 struct _Iterator
539 {
540 template<typename _Tp>
541 using __maybe_const = __conditional_t<_Const, const _Tp, _Tp>;
542
543 using __ptr_traits = pointer_traits<_ValPtr>;
544 using value_type = typename __ptr_traits::element_type;
545 using reference = __maybe_const<value_type>&;
546 using pointer = __maybe_const<value_type>*;
547
548 using iterator_category = bidirectional_iterator_tag;
549 using difference_type = ptrdiff_t;
550
551 using _Node = __rb_tree::_Node<_ValPtr>;
552 using _Node_base = __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>;
553 using _Base_ptr = typename _Node_base::_Base_ptr;
554
555 _Iterator() noexcept
556 : _M_node() { }
557
558 constexpr explicit
559 _Iterator(_Base_ptr __x) noexcept
560 : _M_node(__x) { }
561
562 _Iterator(const _Iterator&) = default;
563 _Iterator& operator=(const _Iterator&) = default;
564
565#ifdef __glibcxx_concepts
566 constexpr
567 _Iterator(const _Iterator<false, _ValPtr>& __it) requires _Const
568#else
569 template<bool _OtherConst,
570 typename = __enable_if_t<_Const && !_OtherConst>>
571 constexpr
572 _Iterator(const _Iterator<_OtherConst, _ValPtr>& __it)
573#endif
574 : _M_node(__it._M_node) { }
575
576 [[__nodiscard__]]
577 reference
578 operator*() const noexcept
579 { return *static_cast<_Node&>(*_M_node)._M_valptr(); }
580
581 [[__nodiscard__]]
582 pointer
583 operator->() const noexcept
584 { return static_cast<_Node&>(*_M_node)._M_valptr(); }
585
586 _GLIBCXX14_CONSTEXPR _Iterator&
587 operator++() noexcept
588 {
589 if (_M_node->_M_right)
590 {
591 _M_node = _M_node->_M_right;
592 while (_M_node->_M_left)
593 _M_node = _M_node->_M_left;
594 }
595 else
596 {
597 _Base_ptr __y = _M_node->_M_parent;
598 while (_M_node == __y->_M_right)
599 {
600 _M_node = __y;
601 __y = __y->_M_parent;
602 }
603 if (_M_node->_M_right != __y)
604 _M_node = __y;
605 }
606
607 return *this;
608 }
609
610 _GLIBCXX14_CONSTEXPR _Iterator
611 operator++(int) noexcept
612 {
613 _Iterator __tmp(this->_M_node);
614 ++*this;
615 return __tmp;
616 }
617
618 _GLIBCXX14_CONSTEXPR _Iterator&
619 operator--() noexcept
620 {
621 if (_M_node->_M_color == _S_red
622 && _M_node->_M_parent->_M_parent == _M_node)
623 _M_node = _M_node->_M_right;
624 else if (_M_node->_M_left)
625 {
626 _Base_ptr __y = _M_node->_M_left;
627 while (__y->_M_right)
628 __y = __y->_M_right;
629 _M_node = __y;
630 }
631 else
632 {
633 _Base_ptr __y = _M_node->_M_parent;
634 while (_M_node == __y->_M_left)
635 {
636 _M_node = __y;
637 __y = __y->_M_parent;
638 }
639 _M_node = __y;
640 }
641 return *this;
642 }
643
644 _GLIBCXX14_CONSTEXPR _Iterator
645 operator--(int) noexcept
646 {
647 _Iterator __tmp(this->_M_node);
648 --*this;
649 return __tmp;
650 }
651
652 [[__nodiscard__]]
653 friend bool
654 operator==(const _Iterator& __x, const _Iterator& __y) _GLIBCXX_NOEXCEPT
655 { return __x._M_node == __y._M_node; }
656
657#if ! __cpp_lib_three_way_comparison
658 [[__nodiscard__]]
659 friend bool
660 operator!=(const _Iterator& __x, const _Iterator& __y) _GLIBCXX_NOEXCEPT
661 { return __x._M_node != __y._M_node; }
662#endif
663
664 _Base_ptr _M_node;
665 };
666#endif // USE_ALLOC_PTR_FOR_RB_TREE
667
668 // Determine the node and iterator types used by std::_Rb_tree.
669 template<typename _Val, typename _Ptr>
670 struct _Node_traits;
671
672#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE <= 9000
673 // Specialization for the simple case where the allocator's pointer type
674 // is the same type as value_type*.
675 // For ABI compatibility we can't change the types used for this case.
676 template<typename _Val>
677 struct _Node_traits<_Val, _Val*>
678 {
679 typedef _Rb_tree_node<_Val> _Node;
680 typedef _Node* _Node_ptr;
681 typedef _Rb_tree_node_base _Node_base;
682 typedef _Node_base* _Base_ptr;
683 typedef _Rb_tree_header _Header_t;
684 typedef _Rb_tree_iterator<_Val> _Iterator;
685 typedef _Rb_tree_const_iterator<_Val> _Const_iterator;
686
687 __attribute__((__nonnull__))
688 static void
689 _S_insert_and_rebalance(const bool __insert_left,
690 _Node_base* __x, _Node_base* __p,
691 _Node_base& __header) _GLIBCXX_USE_NOEXCEPT
692 {
693 return _Rb_tree_insert_and_rebalance(__insert_left, __x, __p, __header);
694 }
695
696 __attribute__((__nonnull__,__returns_nonnull__))
697 static _Node_base*
698 _S_rebalance_for_erase(_Node_base* const __z,
699 _Node_base& __header) _GLIBCXX_USE_NOEXCEPT
700 { return _Rb_tree_rebalance_for_erase(__z, __header); }
701 };
702#endif
703
704#if ! _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
705 // Always use the T* specialization.
706 template<typename _Val, typename _Ptr>
707 struct _Node_traits
708 : _Node_traits<_Val, _Val*>
709 { };
710#else
711 // Primary template used when the allocator uses fancy pointers.
712 template<typename _Val, typename _ValPtr>
713 struct _Node_traits
714 {
715 using _Node = __rb_tree::_Node<_ValPtr>;
716 using _Node_ptr = __ptr_rebind<_ValPtr, _Node>;
717 using _Node_base = __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>;
718 using _Base_ptr = __ptr_rebind<_ValPtr, _Node_base>;
719 using _Header_t = __rb_tree::_Header<_Node_base>;
720 using _Iterator = __rb_tree::_Iterator<false, _ValPtr>;
721 using _Const_iterator = __rb_tree::_Iterator<true, _ValPtr>;
722
723 static void
724 _Rotate_left(_Base_ptr __x, _Base_ptr& __root)
725 {
726 const _Base_ptr __y = __x->_M_right;
727
728 __x->_M_right = __y->_M_left;
729 if (__y->_M_left)
730 __y->_M_left->_M_parent = __x;
731 __y->_M_parent = __x->_M_parent;
732
733 if (__x == __root)
734 __root = __y;
735 else if (__x == __x->_M_parent->_M_left)
736 __x->_M_parent->_M_left = __y;
737 else
738 __x->_M_parent->_M_right = __y;
739 __y->_M_left = __x;
740 __x->_M_parent = __y;
741 }
742
743 static void
744 _Rotate_right(_Base_ptr __x, _Base_ptr& __root)
745 {
746 const _Base_ptr __y = __x->_M_left;
747
748 __x->_M_left = __y->_M_right;
749 if (__y->_M_right)
750 __y->_M_right->_M_parent = __x;
751 __y->_M_parent = __x->_M_parent;
752
753 if (__x == __root)
754 __root = __y;
755 else if (__x == __x->_M_parent->_M_right)
756 __x->_M_parent->_M_right = __y;
757 else
758 __x->_M_parent->_M_left = __y;
759 __y->_M_right = __x;
760 __x->_M_parent = __y;
761 }
762
763 static void
764 _S_insert_and_rebalance(const bool __insert_left,
765 _Base_ptr __x, _Base_ptr __p,
766 _Node_base& __header)
767 {
768 _Base_ptr& __root = __header._M_parent;
769
770 // Initialize fields in new node to insert.
771 __x->_M_parent = __p;
772 __x->_M_left = __x->_M_right = nullptr;
773 __x->_M_color = _S_red;
774
775 // Insert.
776 // Make new node child of parent and maintain root, leftmost and
777 // rightmost nodes.
778 // N.B. First node is always inserted left.
779 if (__insert_left)
780 {
781 __p->_M_left = __x; // also makes leftmost = __x when __p == &__header
782
783 if (std::__to_address(__p) == std::addressof(__header))
784 {
785 __header._M_parent = __x;
786 __header._M_right = __x;
787 }
788 else if (__p == __header._M_left)
789 __header._M_left = __x; // maintain leftmost pointing to min node
790 }
791 else
792 {
793 __p->_M_right = __x;
794
795 if (__p == __header._M_right)
796 __header._M_right = __x; // maintain rightmost pointing to max node
797 }
798 // Rebalance.
799 while (__x != __root
800 && __x->_M_parent->_M_color == _S_red)
801 {
802 const _Base_ptr __xpp = __x->_M_parent->_M_parent;
803
804 if (__x->_M_parent == __xpp->_M_left)
805 {
806 const _Base_ptr __y = __xpp->_M_right;
807 if (__y && __y->_M_color == _S_red)
808 {
809 __x->_M_parent->_M_color = _S_black;
810 __y->_M_color = _S_black;
811 __xpp->_M_color = _S_red;
812 __x = __xpp;
813 }
814 else
815 {
816 if (__x == __x->_M_parent->_M_right)
817 {
818 __x = __x->_M_parent;
819 _Rotate_left(__x, __root);
820 }
821 __x->_M_parent->_M_color = _S_black;
822 __xpp->_M_color = _S_red;
823 _Rotate_right(__xpp, __root);
824 }
825 }
826 else
827 {
828 const _Base_ptr __y = __xpp->_M_left;
829 if (__y && __y->_M_color == _S_red)
830 {
831 __x->_M_parent->_M_color = _S_black;
832 __y->_M_color = _S_black;
833 __xpp->_M_color = _S_red;
834 __x = __xpp;
835 }
836 else
837 {
838 if (__x == __x->_M_parent->_M_left)
839 {
840 __x = __x->_M_parent;
841 _Rotate_right(__x, __root);
842 }
843 __x->_M_parent->_M_color = _S_black;
844 __xpp->_M_color = _S_red;
845 _Rotate_left(__xpp, __root);
846 }
847 }
848 }
849 __root->_M_color = _S_black;
850 }
851
852 static _Base_ptr
853 _S_rebalance_for_erase(_Base_ptr __z, _Node_base& __header)
854 {
855 _Base_ptr& __root = __header._M_parent;
856 _Base_ptr& __leftmost = __header._M_left;
857 _Base_ptr& __rightmost = __header._M_right;
858 _Base_ptr __y = __z;
859 _Base_ptr __x{};
860 _Base_ptr __x_parent{};
861
862 if (!__y->_M_left) // __z has at most one non-null child. y == z.
863 __x = __y->_M_right; // __x might be null.
864 else
865 if (!__y->_M_right) // __z has exactly one non-null child. y == z.
866 __x = __y->_M_left; // __x is not null.
867 else
868 {
869 // __z has two non-null children. Set __y to
870 __y = __y->_M_right; // __z's successor. __x might be null.
871 while (__y->_M_left)
872 __y = __y->_M_left;
873 __x = __y->_M_right;
874 }
875 if (__y != __z)
876 {
877 // relink y in place of z. y is z's successor
878 __z->_M_left->_M_parent = __y;
879 __y->_M_left = __z->_M_left;
880 if (__y != __z->_M_right)
881 {
882 __x_parent = __y->_M_parent;
883 if (__x)
884 __x->_M_parent = __y->_M_parent;
885 __y->_M_parent->_M_left = __x; // __y must be a child of _M_left
886 __y->_M_right = __z->_M_right;
887 __z->_M_right->_M_parent = __y;
888 }
889 else
890 __x_parent = __y;
891 if (__root == __z)
892 __root = __y;
893 else if (__z->_M_parent->_M_left == __z)
894 __z->_M_parent->_M_left = __y;
895 else
896 __z->_M_parent->_M_right = __y;
897 __y->_M_parent = __z->_M_parent;
898 std::swap(__y->_M_color, __z->_M_color);
899 __y = __z;
900 // __y now points to node to be actually deleted
901 }
902 else
903 { // __y == __z
904 __x_parent = __y->_M_parent;
905 if (__x)
906 __x->_M_parent = __y->_M_parent;
907 if (__root == __z)
908 __root = __x;
909 else
910 if (__z->_M_parent->_M_left == __z)
911 __z->_M_parent->_M_left = __x;
912 else
913 __z->_M_parent->_M_right = __x;
914 if (__leftmost == __z)
915 {
916 if (!__z->_M_right) // __z->_M_left must be null also
917 __leftmost = __z->_M_parent;
918 // makes __leftmost == _M_header if __z == __root
919 else
920 __leftmost = _Node_base::_S_minimum(__x);
921 }
922 if (__rightmost == __z)
923 {
924 if (__z->_M_left == 0) // __z->_M_right must be null also
925 __rightmost = __z->_M_parent;
926 // makes __rightmost == _M_header if __z == __root
927 else // __x == __z->_M_left
928 __rightmost = _Node_base::_S_maximum(__x);
929 }
930 }
931 if (__y->_M_color != _S_red)
932 {
933 while (__x != __root && (__x == 0 || __x->_M_color == _S_black))
934 if (__x == __x_parent->_M_left)
935 {
936 _Base_ptr __w = __x_parent->_M_right;
937 if (__w->_M_color == _S_red)
938 {
939 __w->_M_color = _S_black;
940 __x_parent->_M_color = _S_red;
941 _Rotate_left(__x_parent, __root);
942 __w = __x_parent->_M_right;
943 }
944 if ((!__w->_M_left || __w->_M_left->_M_color == _S_black) &&
945 (!__w->_M_right || __w->_M_right->_M_color == _S_black))
946 {
947 __w->_M_color = _S_red;
948 __x = __x_parent;
949 __x_parent = __x_parent->_M_parent;
950 }
951 else
952 {
953 if (!__w->_M_right || __w->_M_right->_M_color == _S_black)
954 {
955 __w->_M_left->_M_color = _S_black;
956 __w->_M_color = _S_red;
957 _Rotate_right(__w, __root);
958 __w = __x_parent->_M_right;
959 }
960 __w->_M_color = __x_parent->_M_color;
961 __x_parent->_M_color = _S_black;
962 if (__w->_M_right)
963 __w->_M_right->_M_color = _S_black;
964 _Rotate_left(__x_parent, __root);
965 break;
966 }
967 }
968 else
969 {
970 // same as above, with _M_right <-> _M_left.
971 _Base_ptr __w = __x_parent->_M_left;
972 if (__w->_M_color == _S_red)
973 {
974 __w->_M_color = _S_black;
975 __x_parent->_M_color = _S_red;
976 _Rotate_right(__x_parent, __root);
977 __w = __x_parent->_M_left;
978 }
979 if ((!__w->_M_right || __w->_M_right->_M_color == _S_black) &&
980 (!__w->_M_left || __w->_M_left->_M_color == _S_black))
981 {
982 __w->_M_color = _S_red;
983 __x = __x_parent;
984 __x_parent = __x_parent->_M_parent;
985 }
986 else
987 {
988 if (!__w->_M_left || __w->_M_left->_M_color == _S_black)
989 {
990 __w->_M_right->_M_color = _S_black;
991 __w->_M_color = _S_red;
992 _Rotate_left(__w, __root);
993 __w = __x_parent->_M_left;
994 }
995 __w->_M_color = __x_parent->_M_color;
996 __x_parent->_M_color = _S_black;
997 if (__w->_M_left)
998 __w->_M_left->_M_color = _S_black;
999 _Rotate_right(__x_parent, __root);
1000 break;
1001 }
1002 }
1003 if (__x)
1004 __x->_M_color = _S_black;
1005 }
1006
1007 return __y;
1008 }
1009 };
1010#endif
1011} // namespace __rb_tree
1012
1013#ifdef __glibcxx_node_extract // >= C++17
1014 template<typename _Tree1, typename _Cmp2>
1015 struct _Rb_tree_merge_helper { };
1016#endif
1017
1018 template<typename _Key, typename _Val, typename _KeyOfValue,
1019 typename _Compare, typename _Alloc = allocator<_Val> >
1020 class _Rb_tree
1021 {
1022 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
1023 rebind<_Val>::other _Val_alloc_type;
1024
1025 typedef __gnu_cxx::__alloc_traits<_Val_alloc_type> _Val_alloc_traits;
1026 typedef typename _Val_alloc_traits::pointer _ValPtr;
1027 typedef __rb_tree::_Node_traits<_Val, _ValPtr> _Node_traits;
1028
1029 typedef typename _Node_traits::_Node_base _Node_base;
1030 typedef typename _Node_traits::_Node _Node;
1031
1032 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
1033 rebind<_Node>::other _Node_allocator;
1034
1035 typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Node_alloc_traits;
1036
1037 protected:
1038 typedef typename _Node_traits::_Base_ptr _Base_ptr;
1039 typedef typename _Node_traits::_Node_ptr _Node_ptr;
1040
1041 private:
1042 // Functor recycling a pool of nodes and using allocation once the pool
1043 // is empty.
1044 struct _Reuse_or_alloc_node
1045 {
1046 _Reuse_or_alloc_node(_Rb_tree& __t)
1047 : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
1048 {
1049 if (_M_root)
1050 {
1051 _M_root->_M_parent = _Base_ptr();
1052
1053 if (_M_nodes->_M_left)
1054 _M_nodes = _M_nodes->_M_left;
1055 }
1056 else
1057 _M_nodes = _Base_ptr();
1058 }
1059
1060#if __cplusplus >= 201103L
1061 _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
1062#endif
1063
1064 ~_Reuse_or_alloc_node()
1065 {
1066 if (_M_root)
1067 _M_t._M_erase(static_cast<_Node&>(*_M_root)._M_node_ptr());
1068 }
1069
1070 template<typename _Arg>
1071 _Node_ptr
1072 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
1073 {
1074 _Base_ptr __base = _M_extract();
1075 if (__base)
1076 {
1077 _Node_ptr __node = static_cast<_Node&>(*__base)._M_node_ptr();
1078 _M_t._M_destroy_node(__node);
1079 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
1080 return __node;
1081 }
1082
1083 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
1084 }
1085
1086 private:
1087 _Base_ptr
1088 _M_extract()
1089 {
1090 if (!_M_nodes)
1091 return _M_nodes;
1092
1093 _Base_ptr __node = _M_nodes;
1094 _M_nodes = _M_nodes->_M_parent;
1095 if (_M_nodes)
1096 {
1097 if (_M_nodes->_M_right == __node)
1098 {
1099 _M_nodes->_M_right = _Base_ptr();
1100
1101 if (_M_nodes->_M_left)
1102 {
1103 _M_nodes = _M_nodes->_M_left;
1104
1105 while (_M_nodes->_M_right)
1106 _M_nodes = _M_nodes->_M_right;
1107
1108 if (_M_nodes->_M_left)
1109 _M_nodes = _M_nodes->_M_left;
1110 }
1111 }
1112 else // __node is on the left.
1113 _M_nodes->_M_left = _Base_ptr();
1114 }
1115 else
1116 _M_root = _Base_ptr();
1117
1118 return __node;
1119 }
1120
1121 _Base_ptr _M_root;
1122 _Base_ptr _M_nodes;
1123 _Rb_tree& _M_t;
1124 };
1125
1126 // Functor similar to the previous one but without any pool of nodes to
1127 // recycle.
1128 struct _Alloc_node
1129 {
1130 _Alloc_node(_Rb_tree& __t)
1131 : _M_t(__t) { }
1132
1133 template<typename _Arg>
1134 _Node_ptr
1135 operator()(_GLIBCXX_FWDREF(_Arg) __arg) const
1136 { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
1137
1138 private:
1139 _Rb_tree& _M_t;
1140 };
1141
1142 public:
1143 typedef _Key key_type;
1144 typedef _Val value_type;
1145 typedef value_type* pointer;
1146 typedef const value_type* const_pointer;
1147 typedef value_type& reference;
1148 typedef const value_type& const_reference;
1149 typedef size_t size_type;
1150 typedef ptrdiff_t difference_type;
1151 typedef _Alloc allocator_type;
1152
1153 _Node_allocator&
1154 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
1155 { return this->_M_impl; }
1156
1157 const _Node_allocator&
1158 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
1159 { return this->_M_impl; }
1160
1161 allocator_type
1162 get_allocator() const _GLIBCXX_NOEXCEPT
1163 { return allocator_type(_M_get_Node_allocator()); }
1164
1165 protected:
1166 _Node_ptr
1167 _M_get_node()
1168 {
1169#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1170 return _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
1171#else
1172#pragma GCC diagnostic push
1173#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1174 using __alloc_pointer = typename _Node_alloc_traits::pointer;
1175 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1176 return _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
1177 else
1178 {
1179 auto __ptr =
1180 _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
1181 return std::__to_address(__ptr);
1182 }
1183#pragma GCC diagnostic pop
1184#endif
1185 }
1186
1187 void
1188 _M_put_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1189 {
1190#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1191 _Node_alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1);
1192#else
1193#pragma GCC diagnostic push
1194#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1195 using __alloc_pointer = typename _Node_alloc_traits::pointer;
1196 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1197 _Node_alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1);
1198 else
1199 {
1200 // When not using the allocator's pointer type internally we must
1201 // convert __p to __alloc_pointer so it can be deallocated.
1202 auto __ap = pointer_traits<__alloc_pointer>::pointer_to(*__p);
1203 _Node_alloc_traits::deallocate(_M_get_Node_allocator(), __ap, 1);
1204 }
1205#pragma GCC diagnostic pop
1206#endif
1207 }
1208
1209#if __cplusplus < 201103L
1210 void
1211 _M_construct_node(_Node_ptr __node, const value_type& __x)
1212 {
1213 __try
1214 { get_allocator().construct(__node->_M_valptr(), __x); }
1215 __catch(...)
1216 {
1217 _M_put_node(__node);
1218 __throw_exception_again;
1219 }
1220 }
1221
1222 _Node_ptr
1223 _M_create_node(const value_type& __x)
1224 {
1225 _Node_ptr __tmp = _M_get_node();
1226 _M_construct_node(__tmp, __x);
1227 return __tmp;
1228 }
1229#else
1230 template<typename... _Args>
1231 void
1232 _M_construct_node(_Node_ptr __node, _Args&&... __args)
1233 {
1234 __try
1235 {
1236 ::new(std::addressof(*__node)) _Node;
1237 _Node_alloc_traits::construct(_M_get_Node_allocator(),
1238 __node->_M_valptr(),
1239 std::forward<_Args>(__args)...);
1240 }
1241 __catch(...)
1242 {
1243 __node->~_Node();
1244 _M_put_node(__node);
1245 __throw_exception_again;
1246 }
1247 }
1248
1249 template<typename... _Args>
1250 _Node_ptr
1251 _M_create_node(_Args&&... __args)
1252 {
1253 _Node_ptr __tmp = _M_get_node();
1254 _M_construct_node(__tmp, std::forward<_Args>(__args)...);
1255 return __tmp;
1256 }
1257#endif
1258
1259 void
1260 _M_destroy_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1261 {
1262#if __cplusplus < 201103L
1263 get_allocator().destroy(__p->_M_valptr());
1264#else
1265 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
1266 __p->~_Node();
1267#endif
1268 }
1269
1270 void
1271 _M_drop_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1272 {
1273 _M_destroy_node(__p);
1274 _M_put_node(__p);
1275 }
1276
1277 template<bool _MoveValue, typename _NodeGen>
1278 _Node_ptr
1279 _M_clone_node(_Node_ptr __x, _NodeGen& __node_gen)
1280 {
1281#if __cplusplus >= 201103L
1282 using _Vp = __conditional_t<_MoveValue,
1283 value_type&&,
1284 const value_type&>;
1285#endif
1286 _Node_ptr __tmp
1287 = __node_gen(_GLIBCXX_FORWARD(_Vp, *__x->_M_valptr()));
1288 __tmp->_M_color = __x->_M_color;
1289 __tmp->_M_left = __tmp->_M_right = _Base_ptr();
1290 return __tmp;
1291 }
1292
1293 protected:
1294 typedef typename _Node_traits::_Header_t _Header_t;
1295
1296#if _GLIBCXX_INLINE_VERSION
1297 template<typename _Key_compare>
1298#else
1299 // Unused _Is_pod_comparator is kept as it is part of mangled name.
1300 template<typename _Key_compare,
1301 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
1302#endif
1303 struct _Rb_tree_impl
1304 : public _Node_allocator
1305 , public _Rb_tree_key_compare<_Key_compare>
1306 , public _Header_t
1307 {
1308 typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
1309
1310 _Rb_tree_impl()
1311 _GLIBCXX_NOEXCEPT_IF(
1312 is_nothrow_default_constructible<_Node_allocator>::value
1313 && is_nothrow_default_constructible<_Base_key_compare>::value )
1314 : _Node_allocator()
1315 { }
1316
1317 _Rb_tree_impl(const _Rb_tree_impl& __x)
1318 : _Node_allocator(_Node_alloc_traits::_S_select_on_copy(__x))
1319 , _Base_key_compare(__x._M_key_compare)
1320 , _Header_t()
1321 { }
1322
1323#if __cplusplus < 201103L
1324 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
1325 : _Node_allocator(__a), _Base_key_compare(__comp)
1326 { }
1327#else
1328 _Rb_tree_impl(_Rb_tree_impl&&)
1329 noexcept( is_nothrow_move_constructible<_Base_key_compare>::value )
1330 = default;
1331
1332 explicit
1333 _Rb_tree_impl(_Node_allocator&& __a)
1334 : _Node_allocator(std::move(__a))
1335 { }
1336
1337 _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
1338 : _Node_allocator(std::move(__a)),
1339 _Base_key_compare(std::move(__x)),
1340 _Header_t(std::move(__x))
1341 { }
1342
1343 _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
1344 : _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
1345 { }
1346#endif
1347 };
1348
1349 _Rb_tree_impl<_Compare> _M_impl;
1350
1351 protected:
1352 _Base_ptr&
1353 _M_root() _GLIBCXX_NOEXCEPT
1354 { return this->_M_impl._M_header._M_parent; }
1355
1356 _Base_ptr
1357 _M_root() const _GLIBCXX_NOEXCEPT
1358 { return this->_M_impl._M_header._M_parent; }
1359
1360 _Base_ptr&
1361 _M_leftmost() _GLIBCXX_NOEXCEPT
1362 { return this->_M_impl._M_header._M_left; }
1363
1364 _Base_ptr
1365 _M_leftmost() const _GLIBCXX_NOEXCEPT
1366 { return this->_M_impl._M_header._M_left; }
1367
1368 _Base_ptr&
1369 _M_rightmost() _GLIBCXX_NOEXCEPT
1370 { return this->_M_impl._M_header._M_right; }
1371
1372 _Base_ptr
1373 _M_rightmost() const _GLIBCXX_NOEXCEPT
1374 { return this->_M_impl._M_header._M_right; }
1375
1376 _Base_ptr
1377 _M_begin() const _GLIBCXX_NOEXCEPT
1378 { return this->_M_impl._M_header._M_parent; }
1379
1380 _Node_ptr
1381 _M_begin_node() const _GLIBCXX_NOEXCEPT
1382 {
1383 _Base_ptr __begin = this->_M_impl._M_header._M_parent;
1384 return __begin
1385 ? static_cast<_Node&>(*__begin)._M_node_ptr()
1386 : _Node_ptr();
1387 }
1388
1389 _Base_ptr
1390 _M_end() const _GLIBCXX_NOEXCEPT
1391 { return this->_M_impl._M_header._M_base_ptr(); }
1392
1393 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1394 // 2542. Missing const requirements for associative containers
1395 template<typename _Key1, typename _Key2>
1396 bool
1397 _M_key_compare(const _Key1& __k1, const _Key2& __k2) const
1398 {
1399#if __cplusplus >= 201103L
1400 // Enforce this here with a user-friendly message.
1401 static_assert(
1402 __is_invocable<const _Compare&, const _Key&, const _Key&>::value,
1403 "comparison object must be invocable with two arguments of key type"
1404 );
1405#endif
1406 return _M_impl._M_key_compare(__k1, __k2);
1407 }
1408
1409 static const _Key&
1410 _S_key(const _Node& __node)
1411 { return _KeyOfValue()(*__node._M_valptr()); }
1412
1413 static const _Key&
1414 _S_key(_Base_ptr __x)
1415 { return _S_key(static_cast<const _Node&>(*__x)); }
1416
1417 static const _Key&
1418 _S_key(_Node_ptr __x)
1419 { return _S_key(*__x); }
1420
1421 static _Base_ptr
1422 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1423 { return __x->_M_left; }
1424
1425 static _Node_ptr
1426 _S_left(_Node_ptr __x)
1427 {
1428 return __x->_M_left
1429 ? static_cast<_Node&>(*__x->_M_left)._M_node_ptr()
1430 : _Node_ptr();
1431 }
1432
1433 static _Base_ptr
1434 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1435 { return __x->_M_right; }
1436
1437 static _Node_ptr
1438 _S_right(_Node_ptr __x) _GLIBCXX_NOEXCEPT
1439 {
1440 return __x->_M_right
1441 ? static_cast<_Node&>(*__x->_M_right)._M_node_ptr()
1442 : _Node_ptr();
1443 }
1444
1445 public:
1446 typedef typename _Node_traits::_Iterator iterator;
1447 typedef typename _Node_traits::_Const_iterator const_iterator;
1448
1449 typedef std::reverse_iterator<iterator> reverse_iterator;
1450 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1451
1452#ifdef __glibcxx_node_extract // >= C++17
1453 using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
1454 using insert_return_type = _Node_insert_return<
1455 __conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
1456 node_type>;
1457#endif
1458
1460 _M_get_insert_unique_pos(const key_type& __k);
1461
1463 _M_get_insert_equal_pos(const key_type& __k);
1464
1466 _M_get_insert_hint_unique_pos(const_iterator __pos,
1467 const key_type& __k);
1468
1470 _M_get_insert_hint_equal_pos(const_iterator __pos,
1471 const key_type& __k);
1472
1473 private:
1474#if __cplusplus >= 201103L
1475 template<typename _Arg, typename _NodeGen>
1476 iterator
1477 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
1478
1479 iterator
1480 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Node_ptr __z);
1481
1482 template<typename _Arg>
1483 iterator
1484 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
1485
1486 template<typename _Arg>
1487 iterator
1488 _M_insert_equal_lower(_Arg&& __x);
1489
1490 iterator
1491 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z);
1492
1493 iterator
1494 _M_insert_equal_lower_node(_Node_ptr __z);
1495#else
1496 template<typename _NodeGen>
1497 iterator
1498 _M_insert_(_Base_ptr __x, _Base_ptr __y,
1499 const value_type& __v, _NodeGen&);
1500
1501 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1502 // 233. Insertion hints in associative containers.
1503 iterator
1504 _M_insert_lower(_Base_ptr __y, const value_type& __v);
1505
1506 iterator
1507 _M_insert_equal_lower(const value_type& __x);
1508#endif
1509
1510 enum { __as_lvalue, __as_rvalue };
1511
1512 template<bool _MoveValues, typename _NodeGen>
1513 _Base_ptr
1514 _M_copy(_Node_ptr, _Base_ptr, _NodeGen&);
1515
1516 template<bool _MoveValues, typename _NodeGen>
1517 _Base_ptr
1518 _M_copy(const _Rb_tree& __x, _NodeGen& __gen)
1519 {
1520 _Base_ptr __root =
1521 _M_copy<_MoveValues>(__x._M_begin_node(), _M_end(), __gen);
1522 _M_leftmost() = _Node_base::_S_minimum(__root);
1523 _M_rightmost() = _Node_base::_S_maximum(__root);
1524 _M_impl._M_node_count = __x._M_impl._M_node_count;
1525 return __root;
1526 }
1527
1528 _Base_ptr
1529 _M_copy(const _Rb_tree& __x)
1530 {
1531 _Alloc_node __an(*this);
1532 return _M_copy<__as_lvalue>(__x, __an);
1533 }
1534
1535 void
1536 _M_erase(_Node_ptr __x);
1537
1538 _Base_ptr
1539 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
1540 const _Key& __k) const;
1541
1542 _Base_ptr
1543 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
1544 const _Key& __k) const;
1545
1546 public:
1547 // allocation/deallocation
1548#if __cplusplus < 201103L
1549 _Rb_tree() { }
1550#else
1551 _Rb_tree() = default;
1552#endif
1553
1554 _Rb_tree(const _Compare& __comp,
1555 const allocator_type& __a = allocator_type())
1556 : _M_impl(__comp, _Node_allocator(__a)) { }
1557
1558 _Rb_tree(const _Rb_tree& __x)
1559 : _M_impl(__x._M_impl)
1560 {
1561 if (__x._M_root())
1562 _M_root() = _M_copy(__x);
1563 }
1564
1565#if __cplusplus >= 201103L
1566 _Rb_tree(const allocator_type& __a)
1567 : _M_impl(_Node_allocator(__a))
1568 { }
1569
1570 _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
1571 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
1572 {
1573 if (__x._M_root())
1574 _M_root() = _M_copy(__x);
1575 }
1576
1577 _Rb_tree(_Rb_tree&&) = default;
1578
1579 _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
1580 : _Rb_tree(std::move(__x), _Node_allocator(__a))
1581 { }
1582
1583 private:
1584 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
1585 noexcept(is_nothrow_default_constructible<_Compare>::value)
1586 : _M_impl(std::move(__x._M_impl), std::move(__a))
1587 { }
1588
1589 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
1590 : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
1591 {
1592 if (__x._M_root())
1593 _M_move_data(__x, false_type{});
1594 }
1595
1596 public:
1597 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
1598 noexcept( noexcept(
1601 : _Rb_tree(std::move(__x), std::move(__a),
1602 typename _Node_alloc_traits::is_always_equal{})
1603 { }
1604#endif
1605
1606 ~_Rb_tree() _GLIBCXX_NOEXCEPT
1607 { _M_erase(_M_begin_node()); }
1608
1609 _Rb_tree&
1610 operator=(const _Rb_tree& __x);
1611
1612 // Accessors.
1613 _Compare
1614 key_comp() const
1615 { return _M_impl._M_key_compare; }
1616
1617 iterator
1618 begin() _GLIBCXX_NOEXCEPT
1619 { return iterator(this->_M_impl._M_header._M_left); }
1620
1621 const_iterator
1622 begin() const _GLIBCXX_NOEXCEPT
1623 { return const_iterator(this->_M_impl._M_header._M_left); }
1624
1625 iterator
1626 end() _GLIBCXX_NOEXCEPT
1627 { return iterator(_M_end()); }
1628
1629 const_iterator
1630 end() const _GLIBCXX_NOEXCEPT
1631 { return const_iterator(_M_end()); }
1632
1633 reverse_iterator
1634 rbegin() _GLIBCXX_NOEXCEPT
1635 { return reverse_iterator(end()); }
1636
1637 const_reverse_iterator
1638 rbegin() const _GLIBCXX_NOEXCEPT
1639 { return const_reverse_iterator(end()); }
1640
1641 reverse_iterator
1642 rend() _GLIBCXX_NOEXCEPT
1643 { return reverse_iterator(begin()); }
1644
1645 const_reverse_iterator
1646 rend() const _GLIBCXX_NOEXCEPT
1647 { return const_reverse_iterator(begin()); }
1648
1649 _GLIBCXX_NODISCARD bool
1650 empty() const _GLIBCXX_NOEXCEPT
1651 { return _M_impl._M_node_count == 0; }
1652
1653 size_type
1654 size() const _GLIBCXX_NOEXCEPT
1655 { return _M_impl._M_node_count; }
1656
1657 size_type
1658 max_size() const _GLIBCXX_NOEXCEPT
1659 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1660
1661 void
1662 swap(_Rb_tree& __t)
1663 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
1664
1665 // Insert/erase.
1666#if __cplusplus >= 201103L
1667 template<typename _Arg>
1669 _M_insert_unique(_Arg&& __x);
1670
1671 template<typename _Arg>
1672 iterator
1673 _M_insert_equal(_Arg&& __x);
1674
1675 template<typename _Arg, typename _NodeGen>
1676 iterator
1677 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1678
1679 template<typename _Arg>
1680 iterator
1681 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
1682 {
1683 _Alloc_node __an(*this);
1684 return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
1685 }
1686
1687 template<typename _Arg, typename _NodeGen>
1688 iterator
1689 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1690
1691 template<typename _Arg>
1692 iterator
1693 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
1694 {
1695 _Alloc_node __an(*this);
1696 return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
1697 }
1698
1699 template<typename... _Args>
1701 _M_emplace_unique(_Args&&... __args);
1702
1703 template<typename... _Args>
1704 iterator
1705 _M_emplace_equal(_Args&&... __args);
1706
1707 template<typename... _Args>
1708 iterator
1709 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
1710
1711 template<typename... _Args>
1712 iterator
1713 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
1714
1715 template<typename _Iter>
1716 using __same_value_type
1717 = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
1718
1719 template<typename _InputIterator>
1720 __enable_if_t<__same_value_type<_InputIterator>::value>
1721 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1722 {
1723 _Alloc_node __an(*this);
1724 for (; __first != __last; ++__first)
1725 _M_insert_unique_(end(), *__first, __an);
1726 }
1727
1728 template<typename _InputIterator>
1729 __enable_if_t<!__same_value_type<_InputIterator>::value>
1730 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1731 {
1732 for (; __first != __last; ++__first)
1733 _M_emplace_unique(*__first);
1734 }
1735
1736 template<typename _InputIterator>
1737 __enable_if_t<__same_value_type<_InputIterator>::value>
1738 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1739 {
1740 _Alloc_node __an(*this);
1741 for (; __first != __last; ++__first)
1742 _M_insert_equal_(end(), *__first, __an);
1743 }
1744
1745 template<typename _InputIterator>
1746 __enable_if_t<!__same_value_type<_InputIterator>::value>
1747 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1748 {
1749 for (; __first != __last; ++__first)
1750 _M_emplace_equal(*__first);
1751 }
1752#else
1754 _M_insert_unique(const value_type& __x);
1755
1756 iterator
1757 _M_insert_equal(const value_type& __x);
1758
1759 template<typename _NodeGen>
1760 iterator
1761 _M_insert_unique_(const_iterator __pos, const value_type& __x,
1762 _NodeGen&);
1763
1764 iterator
1765 _M_insert_unique_(const_iterator __pos, const value_type& __x)
1766 {
1767 _Alloc_node __an(*this);
1768 return _M_insert_unique_(__pos, __x, __an);
1769 }
1770
1771 template<typename _NodeGen>
1772 iterator
1773 _M_insert_equal_(const_iterator __pos, const value_type& __x,
1774 _NodeGen&);
1775 iterator
1776 _M_insert_equal_(const_iterator __pos, const value_type& __x)
1777 {
1778 _Alloc_node __an(*this);
1779 return _M_insert_equal_(__pos, __x, __an);
1780 }
1781
1782 template<typename _InputIterator>
1783 void
1784 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1785 {
1786 _Alloc_node __an(*this);
1787 for (; __first != __last; ++__first)
1788 _M_insert_unique_(end(), *__first, __an);
1789 }
1790
1791 template<typename _InputIterator>
1792 void
1793 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1794 {
1795 _Alloc_node __an(*this);
1796 for (; __first != __last; ++__first)
1797 _M_insert_equal_(end(), *__first, __an);
1798 }
1799#endif
1800
1801 private:
1802 void
1803 _M_erase_aux(const_iterator __position);
1804
1805 void
1806 _M_erase_aux(const_iterator __first, const_iterator __last);
1807
1808 public:
1809#if __cplusplus >= 201103L
1810 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1811 // DR 130. Associative erase should return an iterator.
1812 _GLIBCXX_ABI_TAG_CXX11
1813 iterator
1814 erase(const_iterator __position)
1815 {
1816 __glibcxx_assert(__position != end());
1817 const_iterator __result = __position;
1818 ++__result;
1819 _M_erase_aux(__position);
1820 return iterator(__result._M_node);
1821 }
1822
1823 // LWG 2059.
1824 _GLIBCXX_ABI_TAG_CXX11
1825 iterator
1826 erase(iterator __position)
1827 {
1828 __glibcxx_assert(__position != end());
1829 iterator __result = __position;
1830 ++__result;
1831 _M_erase_aux(__position);
1832 return __result;
1833 }
1834#else
1835 void
1836 erase(iterator __position)
1837 {
1838 __glibcxx_assert(__position != end());
1839 _M_erase_aux(__position);
1840 }
1841
1842 void
1843 erase(const_iterator __position)
1844 {
1845 __glibcxx_assert(__position != end());
1846 _M_erase_aux(__position);
1847 }
1848#endif
1849
1850 size_type
1851 erase(const key_type& __x);
1852
1853 size_type
1854 _M_erase_unique(const key_type& __x);
1855
1856#if __cplusplus >= 201103L
1857 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1858 // DR 130. Associative erase should return an iterator.
1859 _GLIBCXX_ABI_TAG_CXX11
1860 iterator
1861 erase(const_iterator __first, const_iterator __last)
1862 {
1863 _M_erase_aux(__first, __last);
1864 return iterator(__last._M_node);
1865 }
1866#else
1867 void
1868 erase(iterator __first, iterator __last)
1869 { _M_erase_aux(__first, __last); }
1870
1871 void
1872 erase(const_iterator __first, const_iterator __last)
1873 { _M_erase_aux(__first, __last); }
1874#endif
1875
1876 void
1877 clear() _GLIBCXX_NOEXCEPT
1878 {
1879 _M_erase(_M_begin_node());
1880 _M_impl._M_reset();
1881 }
1882
1883 // Set operations.
1884 iterator
1885 find(const key_type& __k);
1886
1887 const_iterator
1888 find(const key_type& __k) const;
1889
1890 size_type
1891 count(const key_type& __k) const;
1892
1893 iterator
1894 lower_bound(const key_type& __k)
1895 { return iterator(_M_lower_bound(_M_begin(), _M_end(), __k)); }
1896
1897 const_iterator
1898 lower_bound(const key_type& __k) const
1899 {
1900 return const_iterator
1901 (_M_lower_bound(_M_begin(), _M_end(), __k));
1902 }
1903
1904 iterator
1905 upper_bound(const key_type& __k)
1906 { return iterator(_M_upper_bound(_M_begin(), _M_end(), __k)); }
1907
1908 const_iterator
1909 upper_bound(const key_type& __k) const
1910 {
1911 return const_iterator
1912 (_M_upper_bound(_M_begin(), _M_end(), __k));
1913 }
1914
1916 equal_range(const key_type& __k);
1917
1919 equal_range(const key_type& __k) const;
1920
1921#if __cplusplus >= 201402L
1922 template<typename _Kt,
1923 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1924 iterator
1925 _M_find_tr(const _Kt& __k)
1926 {
1927 const _Rb_tree* __const_this = this;
1928 return iterator(__const_this->_M_find_tr(__k)._M_node);
1929 }
1930
1931 template<typename _Kt,
1932 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1933 const_iterator
1934 _M_find_tr(const _Kt& __k) const
1935 {
1936 const_iterator __j(_M_lower_bound_tr(__k));
1937 if (__j != end() && _M_key_compare(__k, _S_key(__j._M_node)))
1938 __j = end();
1939 return __j;
1940 }
1941
1942 template<typename _Kt,
1943 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1944 size_type
1945 _M_count_tr(const _Kt& __k) const
1946 {
1947 auto __p = _M_equal_range_tr(__k);
1948 return std::distance(__p.first, __p.second);
1949 }
1950
1951 template<typename _Kt,
1952 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1953 _Base_ptr
1954 _M_lower_bound_tr(const _Kt& __k) const
1955 {
1956 auto __x = _M_begin();
1957 auto __y = _M_end();
1958 while (__x)
1959 if (!_M_key_compare(_S_key(__x), __k))
1960 {
1961 __y = __x;
1962 __x = _S_left(__x);
1963 }
1964 else
1965 __x = _S_right(__x);
1966 return __y;
1967 }
1968
1969 template<typename _Kt,
1970 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1971 _Base_ptr
1972 _M_upper_bound_tr(const _Kt& __k) const
1973 {
1974 auto __x = _M_begin();
1975 auto __y = _M_end();
1976 while (__x)
1977 if (_M_key_compare(__k, _S_key(__x)))
1978 {
1979 __y = __x;
1980 __x = _S_left(__x);
1981 }
1982 else
1983 __x = _S_right(__x);
1984 return __y;
1985 }
1986
1987 template<typename _Kt,
1988 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1990 _M_equal_range_tr(const _Kt& __k)
1991 {
1992 const _Rb_tree* __const_this = this;
1993 auto __ret = __const_this->_M_equal_range_tr(__k);
1994 return
1995 { iterator(__ret.first._M_node), iterator(__ret.second._M_node) };
1996 }
1997
1998 template<typename _Kt,
1999 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
2001 _M_equal_range_tr(const _Kt& __k) const
2002 {
2003 const_iterator __low(_M_lower_bound_tr(__k));
2004 auto __high = __low;
2005 auto& __cmp = _M_impl._M_key_compare;
2006 while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
2007 ++__high;
2008 return { __low, __high };
2009 }
2010#endif
2011
2012 // Debugging.
2013 bool
2014 __rb_verify() const;
2015
2016#if __cplusplus >= 201103L
2017 _Rb_tree&
2018 operator=(_Rb_tree&&)
2019 noexcept(_Node_alloc_traits::_S_nothrow_move()
2020 && is_nothrow_move_assignable<_Compare>::value);
2021
2022 template<typename _Iterator>
2023 void
2024 _M_assign_unique(_Iterator, _Iterator);
2025
2026 template<typename _Iterator>
2027 void
2028 _M_assign_equal(_Iterator, _Iterator);
2029
2030 private:
2031 // Move elements from container with equal allocator.
2032 void
2033 _M_move_data(_Rb_tree& __x, true_type)
2034 { _M_impl._M_move_data(__x._M_impl); }
2035
2036 // Move elements from container with possibly non-equal allocator,
2037 // which might result in a copy not a move.
2038 void
2039 _M_move_data(_Rb_tree&, false_type);
2040
2041 // Move assignment from container with equal allocator.
2042 void
2043 _M_move_assign(_Rb_tree&, true_type);
2044
2045 // Move assignment from container with possibly non-equal allocator,
2046 // which might result in a copy not a move.
2047 void
2048 _M_move_assign(_Rb_tree&, false_type);
2049#endif
2050
2051#if __glibcxx_node_extract // >= C++17
2052 static _Node_ptr
2053 _S_adapt(typename _Node_alloc_traits::pointer __ptr)
2054 {
2055#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2056 return __ptr;
2057#else
2058#pragma GCC diagnostic push
2059#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2060 using __alloc_ptr = typename _Node_alloc_traits::pointer;
2061 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2062 return __ptr;
2063 else
2064 return std::__to_address(__ptr);
2065#pragma GCC diagnostic pop
2066#endif
2067 }
2068
2069 public:
2070 /// Re-insert an extracted node.
2071 insert_return_type
2072 _M_reinsert_node_unique(node_type&& __nh)
2073 {
2074 insert_return_type __ret;
2075 if (__nh.empty())
2076 __ret.position = end();
2077 else
2078 {
2079 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2080
2081 auto __res = _M_get_insert_unique_pos(__nh._M_key());
2082 if (__res.second)
2083 {
2084 __ret.position
2085 = _M_insert_node(__res.first, __res.second,
2086 _S_adapt(__nh._M_ptr));
2087 __nh.release();
2088 __ret.inserted = true;
2089 }
2090 else
2091 {
2092 __ret.node = std::move(__nh);
2093 __ret.position = iterator(__res.first);
2094 __ret.inserted = false;
2095 }
2096 }
2097 return __ret;
2098 }
2099
2100 /// Re-insert an extracted node.
2101 iterator
2102 _M_reinsert_node_equal(node_type&& __nh)
2103 {
2104 iterator __ret;
2105 if (__nh.empty())
2106 __ret = end();
2107 else
2108 {
2109 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2110 auto __res = _M_get_insert_equal_pos(__nh._M_key());
2111 if (__res.second)
2112 __ret = _M_insert_node(__res.first, __res.second,
2113 _S_adapt(__nh._M_ptr));
2114 else
2115 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2116 __nh.release();
2117 }
2118 return __ret;
2119 }
2120
2121 /// Re-insert an extracted node.
2122 iterator
2123 _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
2124 {
2125 iterator __ret;
2126 if (__nh.empty())
2127 __ret = end();
2128 else
2129 {
2130 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2131 auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
2132 if (__res.second)
2133 {
2134 __ret = _M_insert_node(__res.first, __res.second,
2135 _S_adapt(__nh._M_ptr));
2136 __nh.release();
2137 }
2138 else
2139 __ret = iterator(__res.first);
2140 }
2141 return __ret;
2142 }
2143
2144 /// Re-insert an extracted node.
2145 iterator
2146 _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
2147 {
2148 iterator __ret;
2149 if (__nh.empty())
2150 __ret = end();
2151 else
2152 {
2153 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2154 auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
2155 if (__res.second)
2156 __ret = _M_insert_node(__res.first, __res.second,
2157 _S_adapt(__nh._M_ptr));
2158 else
2159 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2160 __nh.release();
2161 }
2162 return __ret;
2163 }
2164
2165 /// Extract a node.
2166 node_type
2167 extract(const_iterator __pos)
2168 {
2169 auto __ptr = _Node_traits::_S_rebalance_for_erase
2170 (__pos._M_node, _M_impl._M_header);
2171 --_M_impl._M_node_count;
2172 auto __node_ptr = static_cast<_Node&>(*__ptr)._M_node_ptr();
2173#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2174 return { __node_ptr, _M_get_Node_allocator() };
2175#else
2176#pragma GCC diagnostic push
2177#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2178 using __alloc_ptr = typename _Node_alloc_traits::pointer;
2179 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2180 return { __node_ptr, _M_get_Node_allocator() };
2181 else
2182 {
2183 auto __ap = pointer_traits<__alloc_ptr>::pointer_to(*__node_ptr);
2184 return { __ap, _M_get_Node_allocator() };
2185 }
2186#pragma GCC diagnostic pop
2187#endif
2188 }
2189
2190 /// Extract a node.
2191 node_type
2192 extract(const key_type& __k)
2193 {
2194 node_type __nh;
2195 auto __pos = find(__k);
2196 if (__pos != end())
2197 __nh = extract(const_iterator(__pos));
2198 return __nh;
2199 }
2200
2201 template<typename _Compare2>
2202 using _Compatible_tree
2203 = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
2204
2205 template<typename, typename>
2206 friend struct _Rb_tree_merge_helper;
2207
2208 /// Merge from a compatible container into one with unique keys.
2209 template<typename _Compare2>
2210 void
2211 _M_merge_unique(_Compatible_tree<_Compare2>& __src) noexcept
2212 {
2213 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2214 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2215 {
2216 auto __pos = __i++;
2217 auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
2218 if (__res.second)
2219 {
2220 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2221 auto __ptr = _Node_traits::_S_rebalance_for_erase
2222 (__pos._M_node, __src_impl._M_header);
2223 --__src_impl._M_node_count;
2224 auto __node_ptr = static_cast<_Node&>(*__ptr)._M_node_ptr();
2225 _M_insert_node(__res.first, __res.second, __node_ptr);
2226 }
2227 }
2228 }
2229
2230 /// Merge from a compatible container into one with equivalent keys.
2231 template<typename _Compare2>
2232 void
2233 _M_merge_equal(_Compatible_tree<_Compare2>& __src) noexcept
2234 {
2235 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2236 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2237 {
2238 auto __pos = __i++;
2239 auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
2240 if (__res.second)
2241 {
2242 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2243 auto __ptr = _Node_traits::_S_rebalance_for_erase
2244 (__pos._M_node, __src_impl._M_header);
2245 --__src_impl._M_node_count;
2246 auto __node_ptr = static_cast<_Node&>(*__ptr)._M_node_ptr();
2247 _M_insert_node(__res.first, __res.second, __node_ptr);
2248 }
2249 }
2250 }
2251#endif // C++17 node_extract
2252
2253 friend bool
2254 operator==(const _Rb_tree& __x, const _Rb_tree& __y)
2255 {
2256 return __x.size() == __y.size()
2257 && std::equal(__x.begin(), __x.end(), __y.begin());
2258 }
2259
2260#if __cpp_lib_three_way_comparison
2261 friend auto
2262 operator<=>(const _Rb_tree& __x, const _Rb_tree& __y)
2263 {
2264 if constexpr (requires { typename __detail::__synth3way_t<_Val>; })
2265 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
2266 __y.begin(), __y.end(),
2267 __detail::__synth3way);
2268 }
2269#else
2270 friend bool
2271 operator<(const _Rb_tree& __x, const _Rb_tree& __y)
2272 {
2273 return std::lexicographical_compare(__x.begin(), __x.end(),
2274 __y.begin(), __y.end());
2275 }
2276#endif
2277
2278 private:
2279#if __cplusplus >= 201103L
2280 // An RAII _Node handle
2281 struct _Auto_node
2282 {
2283 template<typename... _Args>
2284 _Auto_node(_Rb_tree& __t, _Args&&... __args)
2285 : _M_t(__t),
2286 _M_node(__t._M_create_node(std::forward<_Args>(__args)...))
2287 { }
2288
2289 ~_Auto_node()
2290 {
2291 if (_M_node)
2292 _M_t._M_drop_node(_M_node);
2293 }
2294
2295 _Auto_node(_Auto_node&& __n)
2296 : _M_t(__n._M_t), _M_node(__n._M_node)
2297 { __n._M_node = nullptr; }
2298
2299 const _Key&
2300 _M_key() const
2301 { return _S_key(_M_node); }
2302
2303 iterator
2304 _M_insert(pair<_Base_ptr, _Base_ptr> __p)
2305 {
2306 auto __it = _M_t._M_insert_node(__p.first, __p.second, _M_node);
2307 _M_node = nullptr;
2308 return __it;
2309 }
2310
2311 iterator
2312 _M_insert_equal_lower()
2313 {
2314 auto __it = _M_t._M_insert_equal_lower_node(_M_node);
2315 _M_node = nullptr;
2316 return __it;
2317 }
2318
2319 _Rb_tree& _M_t;
2320 _Node_ptr _M_node;
2321 };
2322#endif // C++11
2323 };
2324
2325 template<typename _Key, typename _Val, typename _KeyOfValue,
2326 typename _Compare, typename _Alloc>
2327 inline void
2328 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
2329 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
2330 { __x.swap(__y); }
2331
2332#if __cplusplus >= 201103L
2333 template<typename _Key, typename _Val, typename _KeyOfValue,
2334 typename _Compare, typename _Alloc>
2335 void
2336 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2337 _M_move_data(_Rb_tree& __x, false_type)
2338 {
2339 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2340 _M_move_data(__x, true_type());
2341 else
2342 {
2343 constexpr bool __move = !__move_if_noexcept_cond<value_type>::value;
2344 _Alloc_node __an(*this);
2345 _M_root() = _M_copy<__move>(__x, __an);
2346#pragma GCC diagnostic push
2347#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2348 if constexpr (__move)
2349 __x.clear();
2350#pragma GCC diagnostic pop
2351 }
2352 }
2353
2354 template<typename _Key, typename _Val, typename _KeyOfValue,
2355 typename _Compare, typename _Alloc>
2356 inline void
2357 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2358 _M_move_assign(_Rb_tree& __x, true_type)
2359 {
2360 clear();
2361 if (__x._M_root())
2362 _M_move_data(__x, true_type());
2363 std::__alloc_on_move(_M_get_Node_allocator(),
2364 __x._M_get_Node_allocator());
2365 }
2366
2367 template<typename _Key, typename _Val, typename _KeyOfValue,
2368 typename _Compare, typename _Alloc>
2369 void
2370 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2371 _M_move_assign(_Rb_tree& __x, false_type)
2372 {
2373 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2374 return _M_move_assign(__x, true_type{});
2375
2376 // Try to move each node reusing existing nodes and copying __x nodes
2377 // structure.
2378 _Reuse_or_alloc_node __roan(*this);
2379 _M_impl._M_reset();
2380 if (__x._M_root())
2381 {
2382 _M_root() = _M_copy<__as_rvalue>(__x, __roan);
2383 __x.clear();
2384 }
2385 }
2386
2387 template<typename _Key, typename _Val, typename _KeyOfValue,
2388 typename _Compare, typename _Alloc>
2389 inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2390 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2391 operator=(_Rb_tree&& __x)
2392 noexcept(_Node_alloc_traits::_S_nothrow_move()
2394 {
2395 _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
2396 _M_move_assign(__x,
2397 __bool_constant<_Node_alloc_traits::_S_nothrow_move()>());
2398 return *this;
2399 }
2400
2401 template<typename _Key, typename _Val, typename _KeyOfValue,
2402 typename _Compare, typename _Alloc>
2403 template<typename _Iterator>
2404 void
2405 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2406 _M_assign_unique(_Iterator __first, _Iterator __last)
2407 {
2408 _Reuse_or_alloc_node __roan(*this);
2409 _M_impl._M_reset();
2410 for (; __first != __last; ++__first)
2411 _M_insert_unique_(end(), *__first, __roan);
2412 }
2413
2414 template<typename _Key, typename _Val, typename _KeyOfValue,
2415 typename _Compare, typename _Alloc>
2416 template<typename _Iterator>
2417 void
2418 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2419 _M_assign_equal(_Iterator __first, _Iterator __last)
2420 {
2421 _Reuse_or_alloc_node __roan(*this);
2422 _M_impl._M_reset();
2423 for (; __first != __last; ++__first)
2424 _M_insert_equal_(end(), *__first, __roan);
2425 }
2426#endif
2427
2428 template<typename _Key, typename _Val, typename _KeyOfValue,
2429 typename _Compare, typename _Alloc>
2430 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2431 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2432 operator=(const _Rb_tree& __x)
2433 {
2434 if (this != std::__addressof(__x))
2435 {
2436 // Note that _Key may be a constant type.
2437#if __cplusplus >= 201103L
2438 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
2439 {
2440 auto& __this_alloc = this->_M_get_Node_allocator();
2441 auto& __that_alloc = __x._M_get_Node_allocator();
2442 if (!_Node_alloc_traits::_S_always_equal()
2443 && __this_alloc != __that_alloc)
2444 {
2445 // Replacement allocator cannot free existing storage, we need
2446 // to erase nodes first.
2447 clear();
2448 std::__alloc_on_copy(__this_alloc, __that_alloc);
2449 }
2450 }
2451#endif
2452
2453 _Reuse_or_alloc_node __roan(*this);
2454 _M_impl._M_reset();
2455 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
2456 if (__x._M_root())
2457 _M_root() = _M_copy<__as_lvalue>(__x, __roan);
2458 }
2459
2460 return *this;
2461 }
2462
2463 template<typename _Key, typename _Val, typename _KeyOfValue,
2464 typename _Compare, typename _Alloc>
2465#if __cplusplus >= 201103L
2466 template<typename _Arg, typename _NodeGen>
2467#else
2468 template<typename _NodeGen>
2469#endif
2470 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2471 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2472 _M_insert_(_Base_ptr __x, _Base_ptr __p,
2473#if __cplusplus >= 201103L
2474 _Arg&& __v,
2475#else
2476 const _Val& __v,
2477#endif
2478 _NodeGen& __node_gen)
2479 {
2480 bool __insert_left = (__x || __p == _M_end()
2481 || _M_key_compare(_KeyOfValue()(__v),
2482 _S_key(__p)));
2483
2484 _Base_ptr __z =
2485 __node_gen(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2486
2487 _Node_traits::_S_insert_and_rebalance
2488 (__insert_left, __z, __p, this->_M_impl._M_header);
2489 ++_M_impl._M_node_count;
2490 return iterator(__z);
2491 }
2492
2493 template<typename _Key, typename _Val, typename _KeyOfValue,
2494 typename _Compare, typename _Alloc>
2495#if __cplusplus >= 201103L
2496 template<typename _Arg>
2497#endif
2498 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2499 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2500#if __cplusplus >= 201103L
2501 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
2502#else
2503 _M_insert_lower(_Base_ptr __p, const _Val& __v)
2504#endif
2505 {
2506 bool __insert_left = (__p == _M_end()
2507 || !_M_key_compare(_S_key(__p),
2508 _KeyOfValue()(__v)));
2509
2510 _Base_ptr __z =
2511 _M_create_node(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2512 _Node_traits::_S_insert_and_rebalance
2513 (__insert_left, __z, __p, this->_M_impl._M_header);
2514 ++_M_impl._M_node_count;
2515 return iterator(__z);
2516 }
2517
2518 template<typename _Key, typename _Val, typename _KeyOfValue,
2519 typename _Compare, typename _Alloc>
2520#if __cplusplus >= 201103L
2521 template<typename _Arg>
2522#endif
2523 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2524 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2525#if __cplusplus >= 201103L
2526 _M_insert_equal_lower(_Arg&& __v)
2527#else
2528 _M_insert_equal_lower(const _Val& __v)
2529#endif
2530 {
2531 _Base_ptr __x = _M_begin();
2532 _Base_ptr __y = _M_end();
2533 while (__x)
2534 {
2535 __y = __x;
2536 __x = !_M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
2537 _S_left(__x) : _S_right(__x);
2538 }
2539 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
2540 }
2541
2542 template<typename _Key, typename _Val, typename _KoV,
2543 typename _Compare, typename _Alloc>
2544 template<bool _MoveValues, typename _NodeGen>
2545 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Base_ptr
2546 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
2547 _M_copy(_Node_ptr __x, _Base_ptr __p, _NodeGen& __node_gen)
2548 {
2549 // Structural copy. __x and __p must be non-null.
2550 _Node_ptr __top = _M_clone_node<_MoveValues>(__x, __node_gen);
2551 _Base_ptr __top_base = __top->_M_base_ptr();
2552 __top->_M_parent = __p;
2553
2554 __try
2555 {
2556 if (__x->_M_right)
2557 __top->_M_right =
2558 _M_copy<_MoveValues>(_S_right(__x), __top_base, __node_gen);
2559 __p = __top_base;
2560 __x = _S_left(__x);
2561
2562 while (__x)
2563 {
2564 _Base_ptr __y =
2565 _M_clone_node<_MoveValues>(__x, __node_gen)->_M_base_ptr();
2566 __p->_M_left = __y;
2567 __y->_M_parent = __p;
2568 if (__x->_M_right)
2569 __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
2570 __y, __node_gen);
2571 __p = __y;
2572 __x = _S_left(__x);
2573 }
2574 }
2575 __catch(...)
2576 {
2577 _M_erase(__top);
2578 __throw_exception_again;
2579 }
2580 return __top_base;
2581 }
2582
2583 template<typename _Key, typename _Val, typename _KeyOfValue,
2584 typename _Compare, typename _Alloc>
2585 void
2586 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2587 _M_erase(_Node_ptr __x)
2588 {
2589 // Erase without rebalancing.
2590 while (__x)
2591 {
2592 _M_erase(_S_right(__x));
2593 _Node_ptr __y = _S_left(__x);
2594 _M_drop_node(__x);
2595 __x = __y;
2596 }
2597 }
2598
2599 template<typename _Key, typename _Val, typename _KeyOfValue,
2600 typename _Compare, typename _Alloc>
2601 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2602 _Compare, _Alloc>::_Base_ptr
2603 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2604 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
2605 const _Key& __k) const
2606 {
2607 while (__x)
2608 if (!_M_key_compare(_S_key(__x), __k))
2609 __y = __x, __x = _S_left(__x);
2610 else
2611 __x = _S_right(__x);
2612 return __y;
2613 }
2614
2615 template<typename _Key, typename _Val, typename _KeyOfValue,
2616 typename _Compare, typename _Alloc>
2617 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2618 _Compare, _Alloc>::_Base_ptr
2619 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2620 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
2621 const _Key& __k) const
2622 {
2623 while (__x)
2624 if (_M_key_compare(__k, _S_key(__x)))
2625 __y = __x, __x = _S_left(__x);
2626 else
2627 __x = _S_right(__x);
2628 return __y;
2629 }
2630
2631 template<typename _Key, typename _Val, typename _KeyOfValue,
2632 typename _Compare, typename _Alloc>
2633 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2634 _Compare, _Alloc>::iterator,
2635 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2636 _Compare, _Alloc>::iterator>
2637 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2638 equal_range(const _Key& __k)
2639 {
2640 typedef pair<iterator, iterator> _Ret;
2641
2642 _Base_ptr __x = _M_begin();
2643 _Base_ptr __y = _M_end();
2644 while (__x)
2645 {
2646 if (_M_key_compare(_S_key(__x), __k))
2647 __x = _S_right(__x);
2648 else if (_M_key_compare(__k, _S_key(__x)))
2649 __y = __x, __x = _S_left(__x);
2650 else
2651 {
2652 _Base_ptr __xu(__x);
2653 _Base_ptr __yu(__y);
2654 __y = __x, __x = _S_left(__x);
2655 __xu = _S_right(__xu);
2656 return _Ret(iterator(_M_lower_bound(__x, __y, __k)),
2657 iterator(_M_upper_bound(__xu, __yu, __k)));
2658 }
2659 }
2660 return _Ret(iterator(__y), iterator(__y));
2661 }
2662
2663 template<typename _Key, typename _Val, typename _KeyOfValue,
2664 typename _Compare, typename _Alloc>
2665 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2666 _Compare, _Alloc>::const_iterator,
2667 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2668 _Compare, _Alloc>::const_iterator>
2669 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2670 equal_range(const _Key& __k) const
2671 {
2673
2674 _Base_ptr __x = _M_begin();
2675 _Base_ptr __y = _M_end();
2676 while (__x)
2677 {
2678 if (_M_key_compare(_S_key(__x), __k))
2679 __x = _S_right(__x);
2680 else if (_M_key_compare(__k, _S_key(__x)))
2681 __y = __x, __x = _S_left(__x);
2682 else
2683 {
2684 _Base_ptr __xu(__x);
2685 _Base_ptr __yu(__y);
2686 __y = __x, __x = _S_left(__x);
2687 __xu = _S_right(__xu);
2688 return _Ret(const_iterator(_M_lower_bound(__x, __y, __k)),
2689 const_iterator(_M_upper_bound(__xu, __yu, __k)));
2690 }
2691 }
2692 return _Ret(const_iterator(__y), const_iterator(__y));
2693 }
2694
2695 template<typename _Key, typename _Val, typename _KeyOfValue,
2696 typename _Compare, typename _Alloc>
2697 void
2698 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2699 swap(_Rb_tree& __t)
2700 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
2701 {
2702 if (!_M_root())
2703 {
2704 if (__t._M_root())
2705 _M_impl._M_move_data(__t._M_impl);
2706 }
2707 else if (!__t._M_root())
2708 __t._M_impl._M_move_data(_M_impl);
2709 else
2710 {
2711 std::swap(_M_root(),__t._M_root());
2712 std::swap(_M_leftmost(),__t._M_leftmost());
2713 std::swap(_M_rightmost(),__t._M_rightmost());
2714
2715 _M_root()->_M_parent = _M_end();
2716 __t._M_root()->_M_parent = __t._M_end();
2717 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
2718 }
2719 // No need to swap header's color as it does not change.
2720
2721 using std::swap;
2722 swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
2723
2724 _Node_alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2725 __t._M_get_Node_allocator());
2726 }
2727
2728 template<typename _Key, typename _Val, typename _KeyOfValue,
2729 typename _Compare, typename _Alloc>
2730 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2731 _Compare, _Alloc>::_Base_ptr,
2732 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2733 _Compare, _Alloc>::_Base_ptr>
2734 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2735 _M_get_insert_unique_pos(const key_type& __k)
2736 {
2737 typedef pair<_Base_ptr, _Base_ptr> _Res;
2738 _Base_ptr __x = _M_begin();
2739 _Base_ptr __y = _M_end();
2740 bool __comp = true;
2741 while (__x)
2742 {
2743 __y = __x;
2744 __comp = _M_key_compare(__k, _S_key(__x));
2745 __x = __comp ? _S_left(__x) : _S_right(__x);
2746 }
2747 iterator __j = iterator(__y);
2748 if (__comp)
2749 {
2750 if (__j == begin())
2751 return _Res(__x, __y);
2752 else
2753 --__j;
2754 }
2755 if (_M_key_compare(_S_key(__j._M_node), __k))
2756 return _Res(__x, __y);
2757 return _Res(__j._M_node, _Base_ptr());
2758 }
2759
2760 template<typename _Key, typename _Val, typename _KeyOfValue,
2761 typename _Compare, typename _Alloc>
2762 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2763 _Compare, _Alloc>::_Base_ptr,
2764 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2765 _Compare, _Alloc>::_Base_ptr>
2766 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2767 _M_get_insert_equal_pos(const key_type& __k)
2768 {
2769 typedef pair<_Base_ptr, _Base_ptr> _Res;
2770 _Base_ptr __x = _M_begin();
2771 _Base_ptr __y = _M_end();
2772 while (__x)
2773 {
2774 __y = __x;
2775 __x = _M_key_compare(__k, _S_key(__x)) ? _S_left(__x) : _S_right(__x);
2776 }
2777 return _Res(__x, __y);
2778 }
2779
2780 template<typename _Key, typename _Val, typename _KeyOfValue,
2781 typename _Compare, typename _Alloc>
2782#if __cplusplus >= 201103L
2783 template<typename _Arg>
2784#endif
2785 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2786 _Compare, _Alloc>::iterator, bool>
2787 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2788#if __cplusplus >= 201103L
2789 _M_insert_unique(_Arg&& __v)
2790#else
2791 _M_insert_unique(const _Val& __v)
2792#endif
2793 {
2794 typedef pair<iterator, bool> _Res;
2796 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
2797
2798 if (__res.second)
2799 {
2800 _Alloc_node __an(*this);
2801 return _Res(_M_insert_(__res.first, __res.second,
2802 _GLIBCXX_FORWARD(_Arg, __v), __an),
2803 true);
2804 }
2805
2806 return _Res(iterator(__res.first), false);
2807 }
2808
2809 template<typename _Key, typename _Val, typename _KeyOfValue,
2810 typename _Compare, typename _Alloc>
2811#if __cplusplus >= 201103L
2812 template<typename _Arg>
2813#endif
2814 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2815 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2816#if __cplusplus >= 201103L
2817 _M_insert_equal(_Arg&& __v)
2818#else
2819 _M_insert_equal(const _Val& __v)
2820#endif
2821 {
2823 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
2824 _Alloc_node __an(*this);
2825 return _M_insert_(__res.first, __res.second,
2826 _GLIBCXX_FORWARD(_Arg, __v), __an);
2827 }
2828
2829 template<typename _Key, typename _Val, typename _KeyOfValue,
2830 typename _Compare, typename _Alloc>
2831 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2832 _Compare, _Alloc>::_Base_ptr,
2833 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2834 _Compare, _Alloc>::_Base_ptr>
2835 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2836 _M_get_insert_hint_unique_pos(const_iterator __position,
2837 const key_type& __k)
2838 {
2839 typedef pair<_Base_ptr, _Base_ptr> _Res;
2840
2841 // end()
2842 if (__position._M_node == _M_end())
2843 {
2844 if (size() > 0 && _M_key_compare(_S_key(_M_rightmost()), __k))
2845 return _Res(_Base_ptr(), _M_rightmost());
2846 else
2847 return _M_get_insert_unique_pos(__k);
2848 }
2849 else if (_M_key_compare(__k, _S_key(__position._M_node)))
2850 {
2851 // First, try before...
2852 iterator __before(__position._M_node);
2853 if (__position._M_node == _M_leftmost()) // begin()
2854 return _Res(_M_leftmost(), _M_leftmost());
2855 else if (_M_key_compare(_S_key((--__before)._M_node), __k))
2856 {
2857 if (!_S_right(__before._M_node))
2858 return _Res(_Base_ptr(), __before._M_node);
2859 else
2860 return _Res(__position._M_node, __position._M_node);
2861 }
2862 else
2863 return _M_get_insert_unique_pos(__k);
2864 }
2865 else if (_M_key_compare(_S_key(__position._M_node), __k))
2866 {
2867 // ... then try after.
2868 iterator __after(__position._M_node);
2869 if (__position._M_node == _M_rightmost())
2870 return _Res(_Base_ptr(), _M_rightmost());
2871 else if (_M_key_compare(__k, _S_key((++__after)._M_node)))
2872 {
2873 if (!_S_right(__position._M_node))
2874 return _Res(_Base_ptr(), __position._M_node);
2875 else
2876 return _Res(__after._M_node, __after._M_node);
2877 }
2878 else
2879 return _M_get_insert_unique_pos(__k);
2880 }
2881 else
2882 // Equivalent keys.
2883 return _Res(__position._M_node, _Base_ptr());
2884 }
2885
2886 template<typename _Key, typename _Val, typename _KeyOfValue,
2887 typename _Compare, typename _Alloc>
2888#if __cplusplus >= 201103L
2889 template<typename _Arg, typename _NodeGen>
2890#else
2891 template<typename _NodeGen>
2892#endif
2893 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2894 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2895 _M_insert_unique_(const_iterator __position,
2896#if __cplusplus >= 201103L
2897 _Arg&& __v,
2898#else
2899 const _Val& __v,
2900#endif
2901 _NodeGen& __node_gen)
2902 {
2904 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
2905
2906 if (__res.second)
2907 return _M_insert_(__res.first, __res.second,
2908 _GLIBCXX_FORWARD(_Arg, __v),
2909 __node_gen);
2910 return iterator(__res.first);
2911 }
2912
2913 template<typename _Key, typename _Val, typename _KeyOfValue,
2914 typename _Compare, typename _Alloc>
2915 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2916 _Compare, _Alloc>::_Base_ptr,
2917 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2918 _Compare, _Alloc>::_Base_ptr>
2919 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2920 _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
2921 {
2922 typedef pair<_Base_ptr, _Base_ptr> _Res;
2923
2924 // end()
2925 if (__position._M_node == _M_end())
2926 {
2927 if (size() > 0
2928 && !_M_key_compare(__k, _S_key(_M_rightmost())))
2929 return _Res(_Base_ptr(), _M_rightmost());
2930 else
2931 return _M_get_insert_equal_pos(__k);
2932 }
2933 else if (!_M_key_compare(_S_key(__position._M_node), __k))
2934 {
2935 // First, try before...
2936 iterator __before(__position._M_node);
2937 if (__position._M_node == _M_leftmost()) // begin()
2938 return _Res(_M_leftmost(), _M_leftmost());
2939 else if (!_M_key_compare(__k, _S_key((--__before)._M_node)))
2940 {
2941 if (!_S_right(__before._M_node))
2942 return _Res(_Base_ptr(), __before._M_node);
2943 else
2944 return _Res(__position._M_node, __position._M_node);
2945 }
2946 else
2947 return _M_get_insert_equal_pos(__k);
2948 }
2949 else
2950 {
2951 // ... then try after.
2952 iterator __after(__position._M_node);
2953 if (__position._M_node == _M_rightmost())
2954 return _Res(_Base_ptr(), _M_rightmost());
2955 else if (!_M_key_compare(_S_key((++__after)._M_node), __k))
2956 {
2957 if (!_S_right(__position._M_node))
2958 return _Res(_Base_ptr(), __position._M_node);
2959 else
2960 return _Res(__after._M_node, __after._M_node);
2961 }
2962 else
2963 return _Res(_Base_ptr(), _Base_ptr());
2964 }
2965 }
2966
2967 template<typename _Key, typename _Val, typename _KeyOfValue,
2968 typename _Compare, typename _Alloc>
2969#if __cplusplus >= 201103L
2970 template<typename _Arg, typename _NodeGen>
2971#else
2972 template<typename _NodeGen>
2973#endif
2974 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2975 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2976 _M_insert_equal_(const_iterator __position,
2977#if __cplusplus >= 201103L
2978 _Arg&& __v,
2979#else
2980 const _Val& __v,
2981#endif
2982 _NodeGen& __node_gen)
2983 {
2985 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
2986
2987 if (__res.second)
2988 return _M_insert_(__res.first, __res.second,
2989 _GLIBCXX_FORWARD(_Arg, __v),
2990 __node_gen);
2991
2992 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
2993 }
2994
2995#if __cplusplus >= 201103L
2996 template<typename _Key, typename _Val, typename _KeyOfValue,
2997 typename _Compare, typename _Alloc>
2998 auto
2999 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3000 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Node_ptr __z)
3001 -> iterator
3002 {
3003 bool __insert_left = (__x || __p == _M_end()
3004 || _M_key_compare(_S_key(__z), _S_key(__p)));
3005
3006 _Base_ptr __base_z = __z->_M_base_ptr();
3007 _Node_traits::_S_insert_and_rebalance
3008 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3009 ++_M_impl._M_node_count;
3010 return iterator(__base_z);
3011 }
3012
3013 template<typename _Key, typename _Val, typename _KeyOfValue,
3014 typename _Compare, typename _Alloc>
3015 auto
3016 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3017 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z)
3018 -> iterator
3019 {
3020 bool __insert_left = (__p == _M_end()
3021 || !_M_key_compare(_S_key(__p), _S_key(__z)));
3022
3023 _Base_ptr __base_z = __z->_M_base_ptr();
3024 _Node_traits::_S_insert_and_rebalance
3025 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3026 ++_M_impl._M_node_count;
3027 return iterator(__base_z);
3028 }
3029
3030 template<typename _Key, typename _Val, typename _KeyOfValue,
3031 typename _Compare, typename _Alloc>
3032 auto
3033 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3034 _M_insert_equal_lower_node(_Node_ptr __z)
3035 -> iterator
3036 {
3037 _Base_ptr __x = _M_begin();
3038 _Base_ptr __y = _M_end();
3039 while (__x)
3040 {
3041 __y = __x;
3042 __x = !_M_key_compare(_S_key(__x), _S_key(__z)) ?
3043 _S_left(__x) : _S_right(__x);
3044 }
3045 return _M_insert_lower_node(__y, __z);
3046 }
3047
3048 template<typename _Key, typename _Val, typename _KeyOfValue,
3049 typename _Compare, typename _Alloc>
3050 template<typename... _Args>
3051 auto
3052 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3053 _M_emplace_unique(_Args&&... __args)
3055 {
3056 _Auto_node __z(*this, std::forward<_Args>(__args)...);
3057 auto __res = _M_get_insert_unique_pos(__z._M_key());
3058 if (__res.second)
3059 return {__z._M_insert(__res), true};
3060 return {iterator(__res.first), false};
3061 }
3062
3063 template<typename _Key, typename _Val, typename _KeyOfValue,
3064 typename _Compare, typename _Alloc>
3065 template<typename... _Args>
3066 auto
3067 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3068 _M_emplace_equal(_Args&&... __args)
3069 -> iterator
3070 {
3071 _Auto_node __z(*this, std::forward<_Args>(__args)...);
3072 auto __res = _M_get_insert_equal_pos(__z._M_key());
3073 return __z._M_insert(__res);
3074 }
3075
3076 template<typename _Key, typename _Val, typename _KeyOfValue,
3077 typename _Compare, typename _Alloc>
3078 template<typename... _Args>
3079 auto
3080 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3081 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
3082 -> iterator
3083 {
3084 _Auto_node __z(*this, std::forward<_Args>(__args)...);
3085 auto __res = _M_get_insert_hint_unique_pos(__pos, __z._M_key());
3086 if (__res.second)
3087 return __z._M_insert(__res);
3088 return iterator(__res.first);
3089 }
3090
3091 template<typename _Key, typename _Val, typename _KeyOfValue,
3092 typename _Compare, typename _Alloc>
3093 template<typename... _Args>
3094 auto
3095 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3096 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
3097 -> iterator
3098 {
3099 _Auto_node __z(*this, std::forward<_Args>(__args)...);
3100 auto __res = _M_get_insert_hint_equal_pos(__pos, __z._M_key());
3101 if (__res.second)
3102 return __z._M_insert(__res);
3103 return __z._M_insert_equal_lower();
3104 }
3105#endif
3106
3107
3108 template<typename _Key, typename _Val, typename _KeyOfValue,
3109 typename _Compare, typename _Alloc>
3110 void
3111 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3112 _M_erase_aux(const_iterator __position)
3113 {
3114 _Base_ptr __y = _Node_traits::_S_rebalance_for_erase
3115 (__position._M_node, this->_M_impl._M_header);
3116 _M_drop_node(static_cast<_Node&>(*__y)._M_node_ptr());
3117 --_M_impl._M_node_count;
3118 }
3119
3120 template<typename _Key, typename _Val, typename _KeyOfValue,
3121 typename _Compare, typename _Alloc>
3122 void
3123 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3124 _M_erase_aux(const_iterator __first, const_iterator __last)
3125 {
3126 if (__first == begin() && __last == end())
3127 clear();
3128 else
3129 while (__first != __last)
3130 _M_erase_aux(__first++);
3131 }
3132
3133 template<typename _Key, typename _Val, typename _KeyOfValue,
3134 typename _Compare, typename _Alloc>
3135 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3136 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3137 erase(const _Key& __x)
3138 {
3139 pair<iterator, iterator> __p = equal_range(__x);
3140 const size_type __old_size = size();
3141 _M_erase_aux(__p.first, __p.second);
3142 return __old_size - size();
3143 }
3144
3145 template<typename _Key, typename _Val, typename _KeyOfValue,
3146 typename _Compare, typename _Alloc>
3147 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3148 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3149 _M_erase_unique(const _Key& __x)
3150 {
3151 iterator __it = find(__x);
3152 if (__it == end())
3153 return 0;
3154
3155 _M_erase_aux(__it);
3156 return 1;
3157 }
3158
3159 template<typename _Key, typename _Val, typename _KeyOfValue,
3160 typename _Compare, typename _Alloc>
3161 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3162 _Compare, _Alloc>::iterator
3163 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3164 find(const _Key& __k)
3165 {
3166 iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3167 return (__j == end()
3168 || _M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;
3169 }
3170
3171 template<typename _Key, typename _Val, typename _KeyOfValue,
3172 typename _Compare, typename _Alloc>
3173 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3174 _Compare, _Alloc>::const_iterator
3175 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3176 find(const _Key& __k) const
3177 {
3178 const_iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3179 return (__j == end()
3180 || _M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;
3181 }
3182
3183 template<typename _Key, typename _Val, typename _KeyOfValue,
3184 typename _Compare, typename _Alloc>
3185 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3186 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3187 count(const _Key& __k) const
3188 {
3189 pair<const_iterator, const_iterator> __p = equal_range(__k);
3190 const size_type __n = std::distance(__p.first, __p.second);
3191 return __n;
3192 }
3193
3194 _GLIBCXX_PURE unsigned int
3195 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
3196 const _Rb_tree_node_base* __root) throw ();
3197
3198 template<typename _Key, typename _Val, typename _KeyOfValue,
3199 typename _Compare, typename _Alloc>
3200 bool
3201 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
3202 {
3203 if (_M_impl._M_node_count == 0 || begin() == end())
3204 return _M_impl._M_node_count == 0 && begin() == end()
3205 && this->_M_impl._M_header._M_left == _M_end()
3206 && this->_M_impl._M_header._M_right == _M_end();
3207
3208 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
3209 for (const_iterator __it = begin(); __it != end(); ++__it)
3210 {
3211 _Base_ptr __x = __it._M_node;
3212 _Base_ptr __L = _S_left(__x);
3213 _Base_ptr __R = _S_right(__x);
3214
3215 if (__x->_M_color == _S_red)
3216 if ((__L && __L->_M_color == _S_red)
3217 || (__R && __R->_M_color == _S_red))
3218 return false;
3219
3220 if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
3221 return false;
3222 if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
3223 return false;
3224
3225 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
3226 return false;
3227 }
3228
3229 if (_M_leftmost() != _Node_base::_S_minimum(_M_root()))
3230 return false;
3231 if (_M_rightmost() != _Node_base::_S_maximum(_M_root()))
3232 return false;
3233 return true;
3234 }
3235
3236#ifdef __glibcxx_node_extract // >= C++17
3237 // Allow access to internals of compatible _Rb_tree specializations.
3238 template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
3239 typename _Alloc, typename _Cmp2>
3240 struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
3241 _Cmp2>
3242 {
3243 private:
3244 friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
3245
3246 static auto&
3247 _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
3248 { return __tree._M_impl; }
3249 };
3250#endif // C++17
3251
3252_GLIBCXX_END_NAMESPACE_VERSION
3253} // namespace
3254
3255#endif
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:826
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:119
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2670
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
typename pointer_traits< _Ptr >::template rebind< _Tp > __ptr_rebind
Convenience alias for rebinding pointers.
Definition ptr_traits.h:201
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr _Iterator __base(_Iterator __it)
is_nothrow_move_assignable
Definition type_traits:1367
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
Common iterator class.
static constexpr pointer allocate(_Node_allocator &__a, size_type __n)
static constexpr void deallocate(_Node_allocator &__a, pointer __p, size_type __n)
static constexpr size_type max_size(const _Node_allocator &__a) noexcept