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