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