libstdc++
stl_tree.h
Go to the documentation of this file.
1 // RB tree implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 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 #include <bits/stl_algobase.h>
62 #include <bits/allocator.h>
63 #include <bits/stl_function.h>
64 #include <bits/cpp_type_traits.h>
65 
66 namespace std _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 
70  // Red-black tree class, designed for use in implementing STL
71  // associative containers (set, multiset, map, and multimap). The
72  // insertion and deletion algorithms are based on those in Cormen,
73  // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
74  // 1990), except that
75  //
76  // (1) the header cell is maintained with links not only to the root
77  // but also to the leftmost node of the tree, to enable constant
78  // time begin(), and to the rightmost node of the tree, to enable
79  // linear time performance when used with the generic set algorithms
80  // (set_union, etc.)
81  //
82  // (2) when a node being deleted has two children its successor node
83  // is relinked into its place, rather than copied, so that the only
84  // iterators invalidated are those referring to the deleted node.
85 
86  enum _Rb_tree_color { _S_red = false, _S_black = true };
87 
88  struct _Rb_tree_node_base
89  {
90  typedef _Rb_tree_node_base* _Base_ptr;
91  typedef const _Rb_tree_node_base* _Const_Base_ptr;
92 
93  _Rb_tree_color _M_color;
94  _Base_ptr _M_parent;
95  _Base_ptr _M_left;
96  _Base_ptr _M_right;
97 
98  static _Base_ptr
99  _S_minimum(_Base_ptr __x)
100  {
101  while (__x->_M_left != 0) __x = __x->_M_left;
102  return __x;
103  }
104 
105  static _Const_Base_ptr
106  _S_minimum(_Const_Base_ptr __x)
107  {
108  while (__x->_M_left != 0) __x = __x->_M_left;
109  return __x;
110  }
111 
112  static _Base_ptr
113  _S_maximum(_Base_ptr __x)
114  {
115  while (__x->_M_right != 0) __x = __x->_M_right;
116  return __x;
117  }
118 
119  static _Const_Base_ptr
120  _S_maximum(_Const_Base_ptr __x)
121  {
122  while (__x->_M_right != 0) __x = __x->_M_right;
123  return __x;
124  }
125  };
126 
127  template<typename _Val>
128  struct _Rb_tree_node : public _Rb_tree_node_base
129  {
130  typedef _Rb_tree_node<_Val>* _Link_type;
131  _Val _M_value_field;
132 
133 #if __cplusplus >= 201103L
134  template<typename... _Args>
135  _Rb_tree_node(_Args&&... __args)
136  : _Rb_tree_node_base(),
137  _M_value_field(std::forward<_Args>(__args)...) { }
138 #endif
139  };
140 
141  _GLIBCXX_PURE _Rb_tree_node_base*
142  _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
143 
144  _GLIBCXX_PURE const _Rb_tree_node_base*
145  _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
146 
147  _GLIBCXX_PURE _Rb_tree_node_base*
148  _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
149 
150  _GLIBCXX_PURE const _Rb_tree_node_base*
151  _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
152 
153  template<typename _Tp>
154  struct _Rb_tree_iterator
155  {
156  typedef _Tp value_type;
157  typedef _Tp& reference;
158  typedef _Tp* pointer;
159 
160  typedef bidirectional_iterator_tag iterator_category;
161  typedef ptrdiff_t difference_type;
162 
163  typedef _Rb_tree_iterator<_Tp> _Self;
164  typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
165  typedef _Rb_tree_node<_Tp>* _Link_type;
166 
167  _Rb_tree_iterator()
168  : _M_node() { }
169 
170  explicit
171  _Rb_tree_iterator(_Link_type __x)
172  : _M_node(__x) { }
173 
174  reference
175  operator*() const
176  { return static_cast<_Link_type>(_M_node)->_M_value_field; }
177 
178  pointer
179  operator->() const
180  { return std::__addressof(static_cast<_Link_type>
181  (_M_node)->_M_value_field); }
182 
183  _Self&
184  operator++()
185  {
186  _M_node = _Rb_tree_increment(_M_node);
187  return *this;
188  }
189 
190  _Self
191  operator++(int)
192  {
193  _Self __tmp = *this;
194  _M_node = _Rb_tree_increment(_M_node);
195  return __tmp;
196  }
197 
198  _Self&
199  operator--()
200  {
201  _M_node = _Rb_tree_decrement(_M_node);
202  return *this;
203  }
204 
205  _Self
206  operator--(int)
207  {
208  _Self __tmp = *this;
209  _M_node = _Rb_tree_decrement(_M_node);
210  return __tmp;
211  }
212 
213  bool
214  operator==(const _Self& __x) const
215  { return _M_node == __x._M_node; }
216 
217  bool
218  operator!=(const _Self& __x) const
219  { return _M_node != __x._M_node; }
220 
221  _Base_ptr _M_node;
222  };
223 
224  template<typename _Tp>
225  struct _Rb_tree_const_iterator
226  {
227  typedef _Tp value_type;
228  typedef const _Tp& reference;
229  typedef const _Tp* pointer;
230 
231  typedef _Rb_tree_iterator<_Tp> iterator;
232 
233  typedef bidirectional_iterator_tag iterator_category;
234  typedef ptrdiff_t difference_type;
235 
236  typedef _Rb_tree_const_iterator<_Tp> _Self;
237  typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
238  typedef const _Rb_tree_node<_Tp>* _Link_type;
239 
240  _Rb_tree_const_iterator()
241  : _M_node() { }
242 
243  explicit
244  _Rb_tree_const_iterator(_Link_type __x)
245  : _M_node(__x) { }
246 
247  _Rb_tree_const_iterator(const iterator& __it)
248  : _M_node(__it._M_node) { }
249 
250  iterator
251  _M_const_cast() const
252  { return iterator(static_cast<typename iterator::_Link_type>
253  (const_cast<typename iterator::_Base_ptr>(_M_node))); }
254 
255  reference
256  operator*() const
257  { return static_cast<_Link_type>(_M_node)->_M_value_field; }
258 
259  pointer
260  operator->() const
261  { return std::__addressof(static_cast<_Link_type>
262  (_M_node)->_M_value_field); }
263 
264  _Self&
265  operator++()
266  {
267  _M_node = _Rb_tree_increment(_M_node);
268  return *this;
269  }
270 
271  _Self
272  operator++(int)
273  {
274  _Self __tmp = *this;
275  _M_node = _Rb_tree_increment(_M_node);
276  return __tmp;
277  }
278 
279  _Self&
280  operator--()
281  {
282  _M_node = _Rb_tree_decrement(_M_node);
283  return *this;
284  }
285 
286  _Self
287  operator--(int)
288  {
289  _Self __tmp = *this;
290  _M_node = _Rb_tree_decrement(_M_node);
291  return __tmp;
292  }
293 
294  bool
295  operator==(const _Self& __x) const
296  { return _M_node == __x._M_node; }
297 
298  bool
299  operator!=(const _Self& __x) const
300  { return _M_node != __x._M_node; }
301 
302  _Base_ptr _M_node;
303  };
304 
305  template<typename _Val>
306  inline bool
307  operator==(const _Rb_tree_iterator<_Val>& __x,
308  const _Rb_tree_const_iterator<_Val>& __y)
309  { return __x._M_node == __y._M_node; }
310 
311  template<typename _Val>
312  inline bool
313  operator!=(const _Rb_tree_iterator<_Val>& __x,
314  const _Rb_tree_const_iterator<_Val>& __y)
315  { return __x._M_node != __y._M_node; }
316 
317  void
318  _Rb_tree_insert_and_rebalance(const bool __insert_left,
319  _Rb_tree_node_base* __x,
320  _Rb_tree_node_base* __p,
321  _Rb_tree_node_base& __header) throw ();
322 
323  _Rb_tree_node_base*
324  _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
325  _Rb_tree_node_base& __header) throw ();
326 
327 
328  template<typename _Key, typename _Val, typename _KeyOfValue,
329  typename _Compare, typename _Alloc = allocator<_Val> >
330  class _Rb_tree
331  {
332  typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
333  _Node_allocator;
334 
335  protected:
336  typedef _Rb_tree_node_base* _Base_ptr;
337  typedef const _Rb_tree_node_base* _Const_Base_ptr;
338 
339  public:
340  typedef _Key key_type;
341  typedef _Val value_type;
342  typedef value_type* pointer;
343  typedef const value_type* const_pointer;
344  typedef value_type& reference;
345  typedef const value_type& const_reference;
346  typedef _Rb_tree_node<_Val>* _Link_type;
347  typedef const _Rb_tree_node<_Val>* _Const_Link_type;
348  typedef size_t size_type;
349  typedef ptrdiff_t difference_type;
350  typedef _Alloc allocator_type;
351 
352  _Node_allocator&
353  _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
354  { return *static_cast<_Node_allocator*>(&this->_M_impl); }
355 
356  const _Node_allocator&
357  _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
358  { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
359 
360  allocator_type
361  get_allocator() const _GLIBCXX_NOEXCEPT
362  { return allocator_type(_M_get_Node_allocator()); }
363 
364  protected:
365  _Link_type
366  _M_get_node()
367  { return _M_impl._Node_allocator::allocate(1); }
368 
369  void
370  _M_put_node(_Link_type __p)
371  { _M_impl._Node_allocator::deallocate(__p, 1); }
372 
373 #if __cplusplus < 201103L
374  _Link_type
375  _M_create_node(const value_type& __x)
376  {
377  _Link_type __tmp = _M_get_node();
378  __try
379  { get_allocator().construct
380  (std::__addressof(__tmp->_M_value_field), __x); }
381  __catch(...)
382  {
383  _M_put_node(__tmp);
384  __throw_exception_again;
385  }
386  return __tmp;
387  }
388 
389  void
390  _M_destroy_node(_Link_type __p)
391  {
392  get_allocator().destroy(std::__addressof(__p->_M_value_field));
393  _M_put_node(__p);
394  }
395 #else
396  template<typename... _Args>
397  _Link_type
398  _M_create_node(_Args&&... __args)
399  {
400  _Link_type __tmp = _M_get_node();
401  __try
402  {
403  _M_get_Node_allocator().construct(__tmp,
404  std::forward<_Args>(__args)...);
405  }
406  __catch(...)
407  {
408  _M_put_node(__tmp);
409  __throw_exception_again;
410  }
411  return __tmp;
412  }
413 
414  void
415  _M_destroy_node(_Link_type __p)
416  {
417  _M_get_Node_allocator().destroy(__p);
418  _M_put_node(__p);
419  }
420 #endif
421 
422  _Link_type
423  _M_clone_node(_Const_Link_type __x)
424  {
425  _Link_type __tmp = _M_create_node(__x->_M_value_field);
426  __tmp->_M_color = __x->_M_color;
427  __tmp->_M_left = 0;
428  __tmp->_M_right = 0;
429  return __tmp;
430  }
431 
432  protected:
433  template<typename _Key_compare,
434  bool _Is_pod_comparator = __is_pod(_Key_compare)>
435  struct _Rb_tree_impl : public _Node_allocator
436  {
437  _Key_compare _M_key_compare;
438  _Rb_tree_node_base _M_header;
439  size_type _M_node_count; // Keeps track of size of tree.
440 
441  _Rb_tree_impl()
442  : _Node_allocator(), _M_key_compare(), _M_header(),
443  _M_node_count(0)
444  { _M_initialize(); }
445 
446  _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
447  : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
448  _M_node_count(0)
449  { _M_initialize(); }
450 
451 #if __cplusplus >= 201103L
452  _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
453  : _Node_allocator(std::move(__a)), _M_key_compare(__comp),
454  _M_header(), _M_node_count(0)
455  { _M_initialize(); }
456 #endif
457 
458  private:
459  void
460  _M_initialize()
461  {
462  this->_M_header._M_color = _S_red;
463  this->_M_header._M_parent = 0;
464  this->_M_header._M_left = &this->_M_header;
465  this->_M_header._M_right = &this->_M_header;
466  }
467  };
468 
469  _Rb_tree_impl<_Compare> _M_impl;
470 
471  protected:
472  _Base_ptr&
473  _M_root()
474  { return this->_M_impl._M_header._M_parent; }
475 
476  _Const_Base_ptr
477  _M_root() const
478  { return this->_M_impl._M_header._M_parent; }
479 
480  _Base_ptr&
481  _M_leftmost()
482  { return this->_M_impl._M_header._M_left; }
483 
484  _Const_Base_ptr
485  _M_leftmost() const
486  { return this->_M_impl._M_header._M_left; }
487 
488  _Base_ptr&
489  _M_rightmost()
490  { return this->_M_impl._M_header._M_right; }
491 
492  _Const_Base_ptr
493  _M_rightmost() const
494  { return this->_M_impl._M_header._M_right; }
495 
496  _Link_type
497  _M_begin()
498  { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
499 
500  _Const_Link_type
501  _M_begin() const
502  {
503  return static_cast<_Const_Link_type>
504  (this->_M_impl._M_header._M_parent);
505  }
506 
507  _Link_type
508  _M_end()
509  { return static_cast<_Link_type>(&this->_M_impl._M_header); }
510 
511  _Const_Link_type
512  _M_end() const
513  { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
514 
515  static const_reference
516  _S_value(_Const_Link_type __x)
517  { return __x->_M_value_field; }
518 
519  static const _Key&
520  _S_key(_Const_Link_type __x)
521  { return _KeyOfValue()(_S_value(__x)); }
522 
523  static _Link_type
524  _S_left(_Base_ptr __x)
525  { return static_cast<_Link_type>(__x->_M_left); }
526 
527  static _Const_Link_type
528  _S_left(_Const_Base_ptr __x)
529  { return static_cast<_Const_Link_type>(__x->_M_left); }
530 
531  static _Link_type
532  _S_right(_Base_ptr __x)
533  { return static_cast<_Link_type>(__x->_M_right); }
534 
535  static _Const_Link_type
536  _S_right(_Const_Base_ptr __x)
537  { return static_cast<_Const_Link_type>(__x->_M_right); }
538 
539  static const_reference
540  _S_value(_Const_Base_ptr __x)
541  { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
542 
543  static const _Key&
544  _S_key(_Const_Base_ptr __x)
545  { return _KeyOfValue()(_S_value(__x)); }
546 
547  static _Base_ptr
548  _S_minimum(_Base_ptr __x)
549  { return _Rb_tree_node_base::_S_minimum(__x); }
550 
551  static _Const_Base_ptr
552  _S_minimum(_Const_Base_ptr __x)
553  { return _Rb_tree_node_base::_S_minimum(__x); }
554 
555  static _Base_ptr
556  _S_maximum(_Base_ptr __x)
557  { return _Rb_tree_node_base::_S_maximum(__x); }
558 
559  static _Const_Base_ptr
560  _S_maximum(_Const_Base_ptr __x)
561  { return _Rb_tree_node_base::_S_maximum(__x); }
562 
563  public:
564  typedef _Rb_tree_iterator<value_type> iterator;
565  typedef _Rb_tree_const_iterator<value_type> const_iterator;
566 
567  typedef std::reverse_iterator<iterator> reverse_iterator;
568  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
569 
570  private:
571  pair<_Base_ptr, _Base_ptr>
572  _M_get_insert_unique_pos(const key_type& __k);
573 
574  pair<_Base_ptr, _Base_ptr>
575  _M_get_insert_equal_pos(const key_type& __k);
576 
577  pair<_Base_ptr, _Base_ptr>
578  _M_get_insert_hint_unique_pos(const_iterator __pos,
579  const key_type& __k);
580 
581  pair<_Base_ptr, _Base_ptr>
582  _M_get_insert_hint_equal_pos(const_iterator __pos,
583  const key_type& __k);
584 
585 #if __cplusplus >= 201103L
586  template<typename _Arg>
587  iterator
588  _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v);
589 
590  iterator
591  _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
592 
593  template<typename _Arg>
594  iterator
595  _M_insert_lower(_Base_ptr __y, _Arg&& __v);
596 
597  template<typename _Arg>
598  iterator
599  _M_insert_equal_lower(_Arg&& __x);
600 
601  iterator
602  _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
603 
604  iterator
605  _M_insert_equal_lower_node(_Link_type __z);
606 #else
607  iterator
608  _M_insert_(_Base_ptr __x, _Base_ptr __y,
609  const value_type& __v);
610 
611  // _GLIBCXX_RESOLVE_LIB_DEFECTS
612  // 233. Insertion hints in associative containers.
613  iterator
614  _M_insert_lower(_Base_ptr __y, const value_type& __v);
615 
616  iterator
617  _M_insert_equal_lower(const value_type& __x);
618 #endif
619 
620  _Link_type
621  _M_copy(_Const_Link_type __x, _Link_type __p);
622 
623  void
624  _M_erase(_Link_type __x);
625 
626  iterator
627  _M_lower_bound(_Link_type __x, _Link_type __y,
628  const _Key& __k);
629 
630  const_iterator
631  _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
632  const _Key& __k) const;
633 
634  iterator
635  _M_upper_bound(_Link_type __x, _Link_type __y,
636  const _Key& __k);
637 
638  const_iterator
639  _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
640  const _Key& __k) const;
641 
642  public:
643  // allocation/deallocation
644  _Rb_tree() { }
645 
646  _Rb_tree(const _Compare& __comp,
647  const allocator_type& __a = allocator_type())
648  : _M_impl(__comp, _Node_allocator(__a)) { }
649 
650  _Rb_tree(const _Rb_tree& __x)
651  : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
652  {
653  if (__x._M_root() != 0)
654  {
655  _M_root() = _M_copy(__x._M_begin(), _M_end());
656  _M_leftmost() = _S_minimum(_M_root());
657  _M_rightmost() = _S_maximum(_M_root());
658  _M_impl._M_node_count = __x._M_impl._M_node_count;
659  }
660  }
661 
662 #if __cplusplus >= 201103L
663  _Rb_tree(_Rb_tree&& __x);
664 #endif
665 
666  ~_Rb_tree() _GLIBCXX_NOEXCEPT
667  { _M_erase(_M_begin()); }
668 
669  _Rb_tree&
670  operator=(const _Rb_tree& __x);
671 
672  // Accessors.
673  _Compare
674  key_comp() const
675  { return _M_impl._M_key_compare; }
676 
677  iterator
678  begin() _GLIBCXX_NOEXCEPT
679  {
680  return iterator(static_cast<_Link_type>
681  (this->_M_impl._M_header._M_left));
682  }
683 
684  const_iterator
685  begin() const _GLIBCXX_NOEXCEPT
686  {
687  return const_iterator(static_cast<_Const_Link_type>
688  (this->_M_impl._M_header._M_left));
689  }
690 
691  iterator
692  end() _GLIBCXX_NOEXCEPT
693  { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
694 
695  const_iterator
696  end() const _GLIBCXX_NOEXCEPT
697  {
698  return const_iterator(static_cast<_Const_Link_type>
699  (&this->_M_impl._M_header));
700  }
701 
702  reverse_iterator
703  rbegin() _GLIBCXX_NOEXCEPT
704  { return reverse_iterator(end()); }
705 
706  const_reverse_iterator
707  rbegin() const _GLIBCXX_NOEXCEPT
708  { return const_reverse_iterator(end()); }
709 
710  reverse_iterator
711  rend() _GLIBCXX_NOEXCEPT
712  { return reverse_iterator(begin()); }
713 
714  const_reverse_iterator
715  rend() const _GLIBCXX_NOEXCEPT
716  { return const_reverse_iterator(begin()); }
717 
718  bool
719  empty() const _GLIBCXX_NOEXCEPT
720  { return _M_impl._M_node_count == 0; }
721 
722  size_type
723  size() const _GLIBCXX_NOEXCEPT
724  { return _M_impl._M_node_count; }
725 
726  size_type
727  max_size() const _GLIBCXX_NOEXCEPT
728  { return _M_get_Node_allocator().max_size(); }
729 
730  void
731  swap(_Rb_tree& __t);
732 
733  // Insert/erase.
734 #if __cplusplus >= 201103L
735  template<typename _Arg>
736  pair<iterator, bool>
737  _M_insert_unique(_Arg&& __x);
738 
739  template<typename _Arg>
740  iterator
741  _M_insert_equal(_Arg&& __x);
742 
743  template<typename _Arg>
744  iterator
745  _M_insert_unique_(const_iterator __position, _Arg&& __x);
746 
747  template<typename _Arg>
748  iterator
749  _M_insert_equal_(const_iterator __position, _Arg&& __x);
750 
751  template<typename... _Args>
752  pair<iterator, bool>
753  _M_emplace_unique(_Args&&... __args);
754 
755  template<typename... _Args>
756  iterator
757  _M_emplace_equal(_Args&&... __args);
758 
759  template<typename... _Args>
760  iterator
761  _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
762 
763  template<typename... _Args>
764  iterator
765  _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
766 #else
767  pair<iterator, bool>
768  _M_insert_unique(const value_type& __x);
769 
770  iterator
771  _M_insert_equal(const value_type& __x);
772 
773  iterator
774  _M_insert_unique_(const_iterator __position, const value_type& __x);
775 
776  iterator
777  _M_insert_equal_(const_iterator __position, const value_type& __x);
778 #endif
779 
780  template<typename _InputIterator>
781  void
782  _M_insert_unique(_InputIterator __first, _InputIterator __last);
783 
784  template<typename _InputIterator>
785  void
786  _M_insert_equal(_InputIterator __first, _InputIterator __last);
787 
788  private:
789  void
790  _M_erase_aux(const_iterator __position);
791 
792  void
793  _M_erase_aux(const_iterator __first, const_iterator __last);
794 
795  public:
796 #if __cplusplus >= 201103L
797  // _GLIBCXX_RESOLVE_LIB_DEFECTS
798  // DR 130. Associative erase should return an iterator.
799  iterator
800  erase(const_iterator __position)
801  {
802  const_iterator __result = __position;
803  ++__result;
804  _M_erase_aux(__position);
805  return __result._M_const_cast();
806  }
807 
808  // LWG 2059.
809  iterator
810  erase(iterator __position)
811  {
812  iterator __result = __position;
813  ++__result;
814  _M_erase_aux(__position);
815  return __result;
816  }
817 #else
818  void
819  erase(iterator __position)
820  { _M_erase_aux(__position); }
821 
822  void
823  erase(const_iterator __position)
824  { _M_erase_aux(__position); }
825 #endif
826  size_type
827  erase(const key_type& __x);
828 
829 #if __cplusplus >= 201103L
830  // _GLIBCXX_RESOLVE_LIB_DEFECTS
831  // DR 130. Associative erase should return an iterator.
832  iterator
833  erase(const_iterator __first, const_iterator __last)
834  {
835  _M_erase_aux(__first, __last);
836  return __last._M_const_cast();
837  }
838 #else
839  void
840  erase(iterator __first, iterator __last)
841  { _M_erase_aux(__first, __last); }
842 
843  void
844  erase(const_iterator __first, const_iterator __last)
845  { _M_erase_aux(__first, __last); }
846 #endif
847  void
848  erase(const key_type* __first, const key_type* __last);
849 
850  void
851  clear() _GLIBCXX_NOEXCEPT
852  {
853  _M_erase(_M_begin());
854  _M_leftmost() = _M_end();
855  _M_root() = 0;
856  _M_rightmost() = _M_end();
857  _M_impl._M_node_count = 0;
858  }
859 
860  // Set operations.
861  iterator
862  find(const key_type& __k);
863 
864  const_iterator
865  find(const key_type& __k) const;
866 
867  size_type
868  count(const key_type& __k) const;
869 
870  iterator
871  lower_bound(const key_type& __k)
872  { return _M_lower_bound(_M_begin(), _M_end(), __k); }
873 
874  const_iterator
875  lower_bound(const key_type& __k) const
876  { return _M_lower_bound(_M_begin(), _M_end(), __k); }
877 
878  iterator
879  upper_bound(const key_type& __k)
880  { return _M_upper_bound(_M_begin(), _M_end(), __k); }
881 
882  const_iterator
883  upper_bound(const key_type& __k) const
884  { return _M_upper_bound(_M_begin(), _M_end(), __k); }
885 
886  pair<iterator, iterator>
887  equal_range(const key_type& __k);
888 
889  pair<const_iterator, const_iterator>
890  equal_range(const key_type& __k) const;
891 
892  // Debugging.
893  bool
894  __rb_verify() const;
895  };
896 
897  template<typename _Key, typename _Val, typename _KeyOfValue,
898  typename _Compare, typename _Alloc>
899  inline bool
900  operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
901  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
902  {
903  return __x.size() == __y.size()
904  && std::equal(__x.begin(), __x.end(), __y.begin());
905  }
906 
907  template<typename _Key, typename _Val, typename _KeyOfValue,
908  typename _Compare, typename _Alloc>
909  inline bool
910  operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
911  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
912  {
913  return std::lexicographical_compare(__x.begin(), __x.end(),
914  __y.begin(), __y.end());
915  }
916 
917  template<typename _Key, typename _Val, typename _KeyOfValue,
918  typename _Compare, typename _Alloc>
919  inline bool
920  operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
921  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
922  { return !(__x == __y); }
923 
924  template<typename _Key, typename _Val, typename _KeyOfValue,
925  typename _Compare, typename _Alloc>
926  inline bool
927  operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
928  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
929  { return __y < __x; }
930 
931  template<typename _Key, typename _Val, typename _KeyOfValue,
932  typename _Compare, typename _Alloc>
933  inline bool
934  operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
935  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
936  { return !(__y < __x); }
937 
938  template<typename _Key, typename _Val, typename _KeyOfValue,
939  typename _Compare, typename _Alloc>
940  inline bool
941  operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
942  const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
943  { return !(__x < __y); }
944 
945  template<typename _Key, typename _Val, typename _KeyOfValue,
946  typename _Compare, typename _Alloc>
947  inline void
948  swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
949  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
950  { __x.swap(__y); }
951 
952 #if __cplusplus >= 201103L
953  template<typename _Key, typename _Val, typename _KeyOfValue,
954  typename _Compare, typename _Alloc>
955  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
956  _Rb_tree(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&& __x)
957  : _M_impl(__x._M_impl._M_key_compare,
958  std::move(__x._M_get_Node_allocator()))
959  {
960  if (__x._M_root() != 0)
961  {
962  _M_root() = __x._M_root();
963  _M_leftmost() = __x._M_leftmost();
964  _M_rightmost() = __x._M_rightmost();
965  _M_root()->_M_parent = _M_end();
966 
967  __x._M_root() = 0;
968  __x._M_leftmost() = __x._M_end();
969  __x._M_rightmost() = __x._M_end();
970 
971  this->_M_impl._M_node_count = __x._M_impl._M_node_count;
972  __x._M_impl._M_node_count = 0;
973  }
974  }
975 #endif
976 
977  template<typename _Key, typename _Val, typename _KeyOfValue,
978  typename _Compare, typename _Alloc>
979  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
980  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
981  operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
982  {
983  if (this != &__x)
984  {
985  // Note that _Key may be a constant type.
986  clear();
987  _M_impl._M_key_compare = __x._M_impl._M_key_compare;
988  if (__x._M_root() != 0)
989  {
990  _M_root() = _M_copy(__x._M_begin(), _M_end());
991  _M_leftmost() = _S_minimum(_M_root());
992  _M_rightmost() = _S_maximum(_M_root());
993  _M_impl._M_node_count = __x._M_impl._M_node_count;
994  }
995  }
996  return *this;
997  }
998 
999  template<typename _Key, typename _Val, typename _KeyOfValue,
1000  typename _Compare, typename _Alloc>
1001 #if __cplusplus >= 201103L
1002  template<typename _Arg>
1003 #endif
1004  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1005  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1006 #if __cplusplus >= 201103L
1007  _M_insert_(_Base_ptr __x, _Base_ptr __p, _Arg&& __v)
1008 #else
1009  _M_insert_(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
1010 #endif
1011  {
1012  bool __insert_left = (__x != 0 || __p == _M_end()
1013  || _M_impl._M_key_compare(_KeyOfValue()(__v),
1014  _S_key(__p)));
1015 
1016  _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
1017 
1018  _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1019  this->_M_impl._M_header);
1020  ++_M_impl._M_node_count;
1021  return iterator(__z);
1022  }
1023 
1024  template<typename _Key, typename _Val, typename _KeyOfValue,
1025  typename _Compare, typename _Alloc>
1026 #if __cplusplus >= 201103L
1027  template<typename _Arg>
1028 #endif
1029  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1030  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1031 #if __cplusplus >= 201103L
1032  _M_insert_lower(_Base_ptr __p, _Arg&& __v)
1033 #else
1034  _M_insert_lower(_Base_ptr __p, const _Val& __v)
1035 #endif
1036  {
1037  bool __insert_left = (__p == _M_end()
1038  || !_M_impl._M_key_compare(_S_key(__p),
1039  _KeyOfValue()(__v)));
1040 
1041  _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
1042 
1043  _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1044  this->_M_impl._M_header);
1045  ++_M_impl._M_node_count;
1046  return iterator(__z);
1047  }
1048 
1049  template<typename _Key, typename _Val, typename _KeyOfValue,
1050  typename _Compare, typename _Alloc>
1051 #if __cplusplus >= 201103L
1052  template<typename _Arg>
1053 #endif
1054  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1055  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1056 #if __cplusplus >= 201103L
1057  _M_insert_equal_lower(_Arg&& __v)
1058 #else
1059  _M_insert_equal_lower(const _Val& __v)
1060 #endif
1061  {
1062  _Link_type __x = _M_begin();
1063  _Link_type __y = _M_end();
1064  while (__x != 0)
1065  {
1066  __y = __x;
1067  __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
1068  _S_left(__x) : _S_right(__x);
1069  }
1070  return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
1071  }
1072 
1073  template<typename _Key, typename _Val, typename _KoV,
1074  typename _Compare, typename _Alloc>
1075  typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1076  _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1077  _M_copy(_Const_Link_type __x, _Link_type __p)
1078  {
1079  // Structural copy. __x and __p must be non-null.
1080  _Link_type __top = _M_clone_node(__x);
1081  __top->_M_parent = __p;
1082 
1083  __try
1084  {
1085  if (__x->_M_right)
1086  __top->_M_right = _M_copy(_S_right(__x), __top);
1087  __p = __top;
1088  __x = _S_left(__x);
1089 
1090  while (__x != 0)
1091  {
1092  _Link_type __y = _M_clone_node(__x);
1093  __p->_M_left = __y;
1094  __y->_M_parent = __p;
1095  if (__x->_M_right)
1096  __y->_M_right = _M_copy(_S_right(__x), __y);
1097  __p = __y;
1098  __x = _S_left(__x);
1099  }
1100  }
1101  __catch(...)
1102  {
1103  _M_erase(__top);
1104  __throw_exception_again;
1105  }
1106  return __top;
1107  }
1108 
1109  template<typename _Key, typename _Val, typename _KeyOfValue,
1110  typename _Compare, typename _Alloc>
1111  void
1112  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1113  _M_erase(_Link_type __x)
1114  {
1115  // Erase without rebalancing.
1116  while (__x != 0)
1117  {
1118  _M_erase(_S_right(__x));
1119  _Link_type __y = _S_left(__x);
1120  _M_destroy_node(__x);
1121  __x = __y;
1122  }
1123  }
1124 
1125  template<typename _Key, typename _Val, typename _KeyOfValue,
1126  typename _Compare, typename _Alloc>
1127  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1128  _Compare, _Alloc>::iterator
1129  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1130  _M_lower_bound(_Link_type __x, _Link_type __y,
1131  const _Key& __k)
1132  {
1133  while (__x != 0)
1134  if (!_M_impl._M_key_compare(_S_key(__x), __k))
1135  __y = __x, __x = _S_left(__x);
1136  else
1137  __x = _S_right(__x);
1138  return iterator(__y);
1139  }
1140 
1141  template<typename _Key, typename _Val, typename _KeyOfValue,
1142  typename _Compare, typename _Alloc>
1143  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1144  _Compare, _Alloc>::const_iterator
1145  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1146  _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
1147  const _Key& __k) const
1148  {
1149  while (__x != 0)
1150  if (!_M_impl._M_key_compare(_S_key(__x), __k))
1151  __y = __x, __x = _S_left(__x);
1152  else
1153  __x = _S_right(__x);
1154  return const_iterator(__y);
1155  }
1156 
1157  template<typename _Key, typename _Val, typename _KeyOfValue,
1158  typename _Compare, typename _Alloc>
1159  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1160  _Compare, _Alloc>::iterator
1161  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1162  _M_upper_bound(_Link_type __x, _Link_type __y,
1163  const _Key& __k)
1164  {
1165  while (__x != 0)
1166  if (_M_impl._M_key_compare(__k, _S_key(__x)))
1167  __y = __x, __x = _S_left(__x);
1168  else
1169  __x = _S_right(__x);
1170  return iterator(__y);
1171  }
1172 
1173  template<typename _Key, typename _Val, typename _KeyOfValue,
1174  typename _Compare, typename _Alloc>
1175  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1176  _Compare, _Alloc>::const_iterator
1177  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1178  _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
1179  const _Key& __k) const
1180  {
1181  while (__x != 0)
1182  if (_M_impl._M_key_compare(__k, _S_key(__x)))
1183  __y = __x, __x = _S_left(__x);
1184  else
1185  __x = _S_right(__x);
1186  return const_iterator(__y);
1187  }
1188 
1189  template<typename _Key, typename _Val, typename _KeyOfValue,
1190  typename _Compare, typename _Alloc>
1191  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1192  _Compare, _Alloc>::iterator,
1193  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1194  _Compare, _Alloc>::iterator>
1195  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1196  equal_range(const _Key& __k)
1197  {
1198  _Link_type __x = _M_begin();
1199  _Link_type __y = _M_end();
1200  while (__x != 0)
1201  {
1202  if (_M_impl._M_key_compare(_S_key(__x), __k))
1203  __x = _S_right(__x);
1204  else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1205  __y = __x, __x = _S_left(__x);
1206  else
1207  {
1208  _Link_type __xu(__x), __yu(__y);
1209  __y = __x, __x = _S_left(__x);
1210  __xu = _S_right(__xu);
1211  return pair<iterator,
1212  iterator>(_M_lower_bound(__x, __y, __k),
1213  _M_upper_bound(__xu, __yu, __k));
1214  }
1215  }
1216  return pair<iterator, iterator>(iterator(__y),
1217  iterator(__y));
1218  }
1219 
1220  template<typename _Key, typename _Val, typename _KeyOfValue,
1221  typename _Compare, typename _Alloc>
1222  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1223  _Compare, _Alloc>::const_iterator,
1224  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1225  _Compare, _Alloc>::const_iterator>
1226  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1227  equal_range(const _Key& __k) const
1228  {
1229  _Const_Link_type __x = _M_begin();
1230  _Const_Link_type __y = _M_end();
1231  while (__x != 0)
1232  {
1233  if (_M_impl._M_key_compare(_S_key(__x), __k))
1234  __x = _S_right(__x);
1235  else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1236  __y = __x, __x = _S_left(__x);
1237  else
1238  {
1239  _Const_Link_type __xu(__x), __yu(__y);
1240  __y = __x, __x = _S_left(__x);
1241  __xu = _S_right(__xu);
1242  return pair<const_iterator,
1243  const_iterator>(_M_lower_bound(__x, __y, __k),
1244  _M_upper_bound(__xu, __yu, __k));
1245  }
1246  }
1247  return pair<const_iterator, const_iterator>(const_iterator(__y),
1248  const_iterator(__y));
1249  }
1250 
1251  template<typename _Key, typename _Val, typename _KeyOfValue,
1252  typename _Compare, typename _Alloc>
1253  void
1254  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1255  swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
1256  {
1257  if (_M_root() == 0)
1258  {
1259  if (__t._M_root() != 0)
1260  {
1261  _M_root() = __t._M_root();
1262  _M_leftmost() = __t._M_leftmost();
1263  _M_rightmost() = __t._M_rightmost();
1264  _M_root()->_M_parent = _M_end();
1265 
1266  __t._M_root() = 0;
1267  __t._M_leftmost() = __t._M_end();
1268  __t._M_rightmost() = __t._M_end();
1269  }
1270  }
1271  else if (__t._M_root() == 0)
1272  {
1273  __t._M_root() = _M_root();
1274  __t._M_leftmost() = _M_leftmost();
1275  __t._M_rightmost() = _M_rightmost();
1276  __t._M_root()->_M_parent = __t._M_end();
1277 
1278  _M_root() = 0;
1279  _M_leftmost() = _M_end();
1280  _M_rightmost() = _M_end();
1281  }
1282  else
1283  {
1284  std::swap(_M_root(),__t._M_root());
1285  std::swap(_M_leftmost(),__t._M_leftmost());
1286  std::swap(_M_rightmost(),__t._M_rightmost());
1287 
1288  _M_root()->_M_parent = _M_end();
1289  __t._M_root()->_M_parent = __t._M_end();
1290  }
1291  // No need to swap header's color as it does not change.
1292  std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
1293  std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
1294 
1295  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1296  // 431. Swapping containers with unequal allocators.
1297  std::__alloc_swap<_Node_allocator>::
1298  _S_do_it(_M_get_Node_allocator(), __t._M_get_Node_allocator());
1299  }
1300 
1301  template<typename _Key, typename _Val, typename _KeyOfValue,
1302  typename _Compare, typename _Alloc>
1303  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1304  _Compare, _Alloc>::_Base_ptr,
1305  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1306  _Compare, _Alloc>::_Base_ptr>
1307  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1308  _M_get_insert_unique_pos(const key_type& __k)
1309  {
1310  typedef pair<_Base_ptr, _Base_ptr> _Res;
1311  _Link_type __x = _M_begin();
1312  _Link_type __y = _M_end();
1313  bool __comp = true;
1314  while (__x != 0)
1315  {
1316  __y = __x;
1317  __comp = _M_impl._M_key_compare(__k, _S_key(__x));
1318  __x = __comp ? _S_left(__x) : _S_right(__x);
1319  }
1320  iterator __j = iterator(__y);
1321  if (__comp)
1322  {
1323  if (__j == begin())
1324  return _Res(__x, __y);
1325  else
1326  --__j;
1327  }
1328  if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
1329  return _Res(__x, __y);
1330  return _Res(__j._M_node, 0);
1331  }
1332 
1333  template<typename _Key, typename _Val, typename _KeyOfValue,
1334  typename _Compare, typename _Alloc>
1335  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1336  _Compare, _Alloc>::_Base_ptr,
1337  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1338  _Compare, _Alloc>::_Base_ptr>
1339  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1340  _M_get_insert_equal_pos(const key_type& __k)
1341  {
1342  typedef pair<_Base_ptr, _Base_ptr> _Res;
1343  _Link_type __x = _M_begin();
1344  _Link_type __y = _M_end();
1345  while (__x != 0)
1346  {
1347  __y = __x;
1348  __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
1349  _S_left(__x) : _S_right(__x);
1350  }
1351  return _Res(__x, __y);
1352  }
1353 
1354  template<typename _Key, typename _Val, typename _KeyOfValue,
1355  typename _Compare, typename _Alloc>
1356 #if __cplusplus >= 201103L
1357  template<typename _Arg>
1358 #endif
1359  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1360  _Compare, _Alloc>::iterator, bool>
1361  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1362 #if __cplusplus >= 201103L
1363  _M_insert_unique(_Arg&& __v)
1364 #else
1365  _M_insert_unique(const _Val& __v)
1366 #endif
1367  {
1368  typedef pair<iterator, bool> _Res;
1369  pair<_Base_ptr, _Base_ptr> __res
1370  = _M_get_insert_unique_pos(_KeyOfValue()(__v));
1371 
1372  if (__res.second)
1373  return _Res(_M_insert_(__res.first, __res.second,
1374  _GLIBCXX_FORWARD(_Arg, __v)),
1375  true);
1376 
1377  return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
1378  }
1379 
1380  template<typename _Key, typename _Val, typename _KeyOfValue,
1381  typename _Compare, typename _Alloc>
1382 #if __cplusplus >= 201103L
1383  template<typename _Arg>
1384 #endif
1385  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1386  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1387 #if __cplusplus >= 201103L
1388  _M_insert_equal(_Arg&& __v)
1389 #else
1390  _M_insert_equal(const _Val& __v)
1391 #endif
1392  {
1393  pair<_Base_ptr, _Base_ptr> __res
1394  = _M_get_insert_equal_pos(_KeyOfValue()(__v));
1395  return _M_insert_(__res.first, __res.second, _GLIBCXX_FORWARD(_Arg, __v));
1396  }
1397 
1398  template<typename _Key, typename _Val, typename _KeyOfValue,
1399  typename _Compare, typename _Alloc>
1400  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1401  _Compare, _Alloc>::_Base_ptr,
1402  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1403  _Compare, _Alloc>::_Base_ptr>
1404  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1405  _M_get_insert_hint_unique_pos(const_iterator __position,
1406  const key_type& __k)
1407  {
1408  iterator __pos = __position._M_const_cast();
1409  typedef pair<_Base_ptr, _Base_ptr> _Res;
1410 
1411  // end()
1412  if (__pos._M_node == _M_end())
1413  {
1414  if (size() > 0
1415  && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
1416  return _Res(0, _M_rightmost());
1417  else
1418  return _M_get_insert_unique_pos(__k);
1419  }
1420  else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
1421  {
1422  // First, try before...
1423  iterator __before = __pos;
1424  if (__pos._M_node == _M_leftmost()) // begin()
1425  return _Res(_M_leftmost(), _M_leftmost());
1426  else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
1427  {
1428  if (_S_right(__before._M_node) == 0)
1429  return _Res(0, __before._M_node);
1430  else
1431  return _Res(__pos._M_node, __pos._M_node);
1432  }
1433  else
1434  return _M_get_insert_unique_pos(__k);
1435  }
1436  else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
1437  {
1438  // ... then try after.
1439  iterator __after = __pos;
1440  if (__pos._M_node == _M_rightmost())
1441  return _Res(0, _M_rightmost());
1442  else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
1443  {
1444  if (_S_right(__pos._M_node) == 0)
1445  return _Res(0, __pos._M_node);
1446  else
1447  return _Res(__after._M_node, __after._M_node);
1448  }
1449  else
1450  return _M_get_insert_unique_pos(__k);
1451  }
1452  else
1453  // Equivalent keys.
1454  return _Res(__pos._M_node, 0);
1455  }
1456 
1457  template<typename _Key, typename _Val, typename _KeyOfValue,
1458  typename _Compare, typename _Alloc>
1459 #if __cplusplus >= 201103L
1460  template<typename _Arg>
1461 #endif
1462  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1463  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1464 #if __cplusplus >= 201103L
1465  _M_insert_unique_(const_iterator __position, _Arg&& __v)
1466 #else
1467  _M_insert_unique_(const_iterator __position, const _Val& __v)
1468 #endif
1469  {
1470  pair<_Base_ptr, _Base_ptr> __res
1471  = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
1472 
1473  if (__res.second)
1474  return _M_insert_(__res.first, __res.second,
1475  _GLIBCXX_FORWARD(_Arg, __v));
1476  return iterator(static_cast<_Link_type>(__res.first));
1477  }
1478 
1479  template<typename _Key, typename _Val, typename _KeyOfValue,
1480  typename _Compare, typename _Alloc>
1481  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1482  _Compare, _Alloc>::_Base_ptr,
1483  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1484  _Compare, _Alloc>::_Base_ptr>
1485  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1486  _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
1487  {
1488  iterator __pos = __position._M_const_cast();
1489  typedef pair<_Base_ptr, _Base_ptr> _Res;
1490 
1491  // end()
1492  if (__pos._M_node == _M_end())
1493  {
1494  if (size() > 0
1495  && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
1496  return _Res(0, _M_rightmost());
1497  else
1498  return _M_get_insert_equal_pos(__k);
1499  }
1500  else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
1501  {
1502  // First, try before...
1503  iterator __before = __pos;
1504  if (__pos._M_node == _M_leftmost()) // begin()
1505  return _Res(_M_leftmost(), _M_leftmost());
1506  else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
1507  {
1508  if (_S_right(__before._M_node) == 0)
1509  return _Res(0, __before._M_node);
1510  else
1511  return _Res(__pos._M_node, __pos._M_node);
1512  }
1513  else
1514  return _M_get_insert_equal_pos(__k);
1515  }
1516  else
1517  {
1518  // ... then try after.
1519  iterator __after = __pos;
1520  if (__pos._M_node == _M_rightmost())
1521  return _Res(0, _M_rightmost());
1522  else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
1523  {
1524  if (_S_right(__pos._M_node) == 0)
1525  return _Res(0, __pos._M_node);
1526  else
1527  return _Res(__after._M_node, __after._M_node);
1528  }
1529  else
1530  return _Res(0, 0);
1531  }
1532  }
1533 
1534  template<typename _Key, typename _Val, typename _KeyOfValue,
1535  typename _Compare, typename _Alloc>
1536 #if __cplusplus >= 201103L
1537  template<typename _Arg>
1538 #endif
1539  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1540  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1541 #if __cplusplus >= 201103L
1542  _M_insert_equal_(const_iterator __position, _Arg&& __v)
1543 #else
1544  _M_insert_equal_(const_iterator __position, const _Val& __v)
1545 #endif
1546  {
1547  pair<_Base_ptr, _Base_ptr> __res
1548  = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
1549 
1550  if (__res.second)
1551  return _M_insert_(__res.first, __res.second,
1552  _GLIBCXX_FORWARD(_Arg, __v));
1553 
1554  return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
1555  }
1556 
1557 #if __cplusplus >= 201103L
1558  template<typename _Key, typename _Val, typename _KeyOfValue,
1559  typename _Compare, typename _Alloc>
1560  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1561  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1562  _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
1563  {
1564  bool __insert_left = (__x != 0 || __p == _M_end()
1565  || _M_impl._M_key_compare(_S_key(__z),
1566  _S_key(__p)));
1567 
1568  _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1569  this->_M_impl._M_header);
1570  ++_M_impl._M_node_count;
1571  return iterator(__z);
1572  }
1573 
1574  template<typename _Key, typename _Val, typename _KeyOfValue,
1575  typename _Compare, typename _Alloc>
1576  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1577  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1578  _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
1579  {
1580  bool __insert_left = (__p == _M_end()
1581  || !_M_impl._M_key_compare(_S_key(__p),
1582  _S_key(__z)));
1583 
1584  _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1585  this->_M_impl._M_header);
1586  ++_M_impl._M_node_count;
1587  return iterator(__z);
1588  }
1589 
1590  template<typename _Key, typename _Val, typename _KeyOfValue,
1591  typename _Compare, typename _Alloc>
1592  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1593  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1594  _M_insert_equal_lower_node(_Link_type __z)
1595  {
1596  _Link_type __x = _M_begin();
1597  _Link_type __y = _M_end();
1598  while (__x != 0)
1599  {
1600  __y = __x;
1601  __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
1602  _S_left(__x) : _S_right(__x);
1603  }
1604  return _M_insert_lower_node(__y, __z);
1605  }
1606 
1607  template<typename _Key, typename _Val, typename _KeyOfValue,
1608  typename _Compare, typename _Alloc>
1609  template<typename... _Args>
1610  pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1611  _Compare, _Alloc>::iterator, bool>
1612  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1613  _M_emplace_unique(_Args&&... __args)
1614  {
1615  _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1616 
1617  __try
1618  {
1619  typedef pair<iterator, bool> _Res;
1620  auto __res = _M_get_insert_unique_pos(_S_key(__z));
1621  if (__res.second)
1622  return _Res(_M_insert_node(__res.first, __res.second, __z), true);
1623 
1624  _M_destroy_node(__z);
1625  return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
1626  }
1627  __catch(...)
1628  {
1629  _M_destroy_node(__z);
1630  __throw_exception_again;
1631  }
1632  }
1633 
1634  template<typename _Key, typename _Val, typename _KeyOfValue,
1635  typename _Compare, typename _Alloc>
1636  template<typename... _Args>
1637  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1638  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1639  _M_emplace_equal(_Args&&... __args)
1640  {
1641  _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1642 
1643  __try
1644  {
1645  auto __res = _M_get_insert_equal_pos(_S_key(__z));
1646  return _M_insert_node(__res.first, __res.second, __z);
1647  }
1648  __catch(...)
1649  {
1650  _M_destroy_node(__z);
1651  __throw_exception_again;
1652  }
1653  }
1654 
1655  template<typename _Key, typename _Val, typename _KeyOfValue,
1656  typename _Compare, typename _Alloc>
1657  template<typename... _Args>
1658  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1659  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1660  _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
1661  {
1662  _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1663 
1664  __try
1665  {
1666  auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
1667 
1668  if (__res.second)
1669  return _M_insert_node(__res.first, __res.second, __z);
1670 
1671  _M_destroy_node(__z);
1672  return iterator(static_cast<_Link_type>(__res.first));
1673  }
1674  __catch(...)
1675  {
1676  _M_destroy_node(__z);
1677  __throw_exception_again;
1678  }
1679  }
1680 
1681  template<typename _Key, typename _Val, typename _KeyOfValue,
1682  typename _Compare, typename _Alloc>
1683  template<typename... _Args>
1684  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1685  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1686  _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
1687  {
1688  _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1689 
1690  __try
1691  {
1692  auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
1693 
1694  if (__res.second)
1695  return _M_insert_node(__res.first, __res.second, __z);
1696 
1697  return _M_insert_equal_lower_node(__z);
1698  }
1699  __catch(...)
1700  {
1701  _M_destroy_node(__z);
1702  __throw_exception_again;
1703  }
1704  }
1705 #endif
1706 
1707  template<typename _Key, typename _Val, typename _KoV,
1708  typename _Cmp, typename _Alloc>
1709  template<class _II>
1710  void
1711  _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1712  _M_insert_unique(_II __first, _II __last)
1713  {
1714  for (; __first != __last; ++__first)
1715  _M_insert_unique_(end(), *__first);
1716  }
1717 
1718  template<typename _Key, typename _Val, typename _KoV,
1719  typename _Cmp, typename _Alloc>
1720  template<class _II>
1721  void
1722  _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1723  _M_insert_equal(_II __first, _II __last)
1724  {
1725  for (; __first != __last; ++__first)
1726  _M_insert_equal_(end(), *__first);
1727  }
1728 
1729  template<typename _Key, typename _Val, typename _KeyOfValue,
1730  typename _Compare, typename _Alloc>
1731  void
1732  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1733  _M_erase_aux(const_iterator __position)
1734  {
1735  _Link_type __y =
1736  static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1737  (const_cast<_Base_ptr>(__position._M_node),
1738  this->_M_impl._M_header));
1739  _M_destroy_node(__y);
1740  --_M_impl._M_node_count;
1741  }
1742 
1743  template<typename _Key, typename _Val, typename _KeyOfValue,
1744  typename _Compare, typename _Alloc>
1745  void
1746  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1747  _M_erase_aux(const_iterator __first, const_iterator __last)
1748  {
1749  if (__first == begin() && __last == end())
1750  clear();
1751  else
1752  while (__first != __last)
1753  erase(__first++);
1754  }
1755 
1756  template<typename _Key, typename _Val, typename _KeyOfValue,
1757  typename _Compare, typename _Alloc>
1758  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1759  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1760  erase(const _Key& __x)
1761  {
1762  pair<iterator, iterator> __p = equal_range(__x);
1763  const size_type __old_size = size();
1764  erase(__p.first, __p.second);
1765  return __old_size - size();
1766  }
1767 
1768  template<typename _Key, typename _Val, typename _KeyOfValue,
1769  typename _Compare, typename _Alloc>
1770  void
1771  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1772  erase(const _Key* __first, const _Key* __last)
1773  {
1774  while (__first != __last)
1775  erase(*__first++);
1776  }
1777 
1778  template<typename _Key, typename _Val, typename _KeyOfValue,
1779  typename _Compare, typename _Alloc>
1780  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1781  _Compare, _Alloc>::iterator
1782  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1783  find(const _Key& __k)
1784  {
1785  iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1786  return (__j == end()
1787  || _M_impl._M_key_compare(__k,
1788  _S_key(__j._M_node))) ? end() : __j;
1789  }
1790 
1791  template<typename _Key, typename _Val, typename _KeyOfValue,
1792  typename _Compare, typename _Alloc>
1793  typename _Rb_tree<_Key, _Val, _KeyOfValue,
1794  _Compare, _Alloc>::const_iterator
1795  _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1796  find(const _Key& __k) const
1797  {
1798  const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1799  return (__j == end()
1800  || _M_impl._M_key_compare(__k,
1801  _S_key(__j._M_node))) ? end() : __j;
1802  }
1803 
1804  template<typename _Key, typename _Val, typename _KeyOfValue,
1805  typename _Compare, typename _Alloc>
1806  typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1808  count(const _Key& __k) const
1809  {
1810  pair<const_iterator, const_iterator> __p = equal_range(__k);
1811  const size_type __n = std::distance(__p.first, __p.second);
1812  return __n;
1813  }
1814 
1815  _GLIBCXX_PURE unsigned int
1816  _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1817  const _Rb_tree_node_base* __root) throw ();
1818 
1819  template<typename _Key, typename _Val, typename _KeyOfValue,
1820  typename _Compare, typename _Alloc>
1821  bool
1822  _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1823  {
1824  if (_M_impl._M_node_count == 0 || begin() == end())
1825  return _M_impl._M_node_count == 0 && begin() == end()
1826  && this->_M_impl._M_header._M_left == _M_end()
1827  && this->_M_impl._M_header._M_right == _M_end();
1828 
1829  unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1830  for (const_iterator __it = begin(); __it != end(); ++__it)
1831  {
1832  _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1833  _Const_Link_type __L = _S_left(__x);
1834  _Const_Link_type __R = _S_right(__x);
1835 
1836  if (__x->_M_color == _S_red)
1837  if ((__L && __L->_M_color == _S_red)
1838  || (__R && __R->_M_color == _S_red))
1839  return false;
1840 
1841  if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1842  return false;
1843  if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1844  return false;
1845 
1846  if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1847  return false;
1848  }
1849 
1850  if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1851  return false;
1852  if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1853  return false;
1854  return true;
1855  }
1856 
1857 _GLIBCXX_END_NAMESPACE_VERSION
1858 } // namespace
1859 
1860 #endif