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