libstdc++

stl_tree.h

Go to the documentation of this file.
00001 // RB tree implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1996,1997
00028  * Silicon Graphics Computer Systems, Inc.
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Silicon Graphics makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1994
00040  * Hewlett-Packard Company
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Hewlett-Packard Company makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  *
00050  *
00051  */
00052 
00053 /** @file bits/stl_tree.h
00054  *  This is an internal header file, included by other library headers.
00055  *  Do not attempt to use it directly. @headername{map,set}
00056  */
00057 
00058 #ifndef _STL_TREE_H
00059 #define _STL_TREE_H 1
00060 
00061 #pragma GCC system_header
00062 
00063 #include <bits/stl_algobase.h>
00064 #include <bits/allocator.h>
00065 #include <bits/stl_function.h>
00066 #include <bits/cpp_type_traits.h>
00067 #include <ext/alloc_traits.h>
00068 #if __cplusplus >= 201103L
00069 #include <ext/aligned_buffer.h>
00070 #endif
00071 
00072 namespace std _GLIBCXX_VISIBILITY(default)
00073 {
00074 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00075 
00076   // Red-black tree class, designed for use in implementing STL
00077   // associative containers (set, multiset, map, and multimap). The
00078   // insertion and deletion algorithms are based on those in Cormen,
00079   // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
00080   // 1990), except that
00081   //
00082   // (1) the header cell is maintained with links not only to the root
00083   // but also to the leftmost node of the tree, to enable constant
00084   // time begin(), and to the rightmost node of the tree, to enable
00085   // linear time performance when used with the generic set algorithms
00086   // (set_union, etc.)
00087   // 
00088   // (2) when a node being deleted has two children its successor node
00089   // is relinked into its place, rather than copied, so that the only
00090   // iterators invalidated are those referring to the deleted node.
00091 
00092   enum _Rb_tree_color { _S_red = false, _S_black = true };
00093 
00094   struct _Rb_tree_node_base
00095   {
00096     typedef _Rb_tree_node_base* _Base_ptr;
00097     typedef const _Rb_tree_node_base* _Const_Base_ptr;
00098 
00099     _Rb_tree_color      _M_color;
00100     _Base_ptr           _M_parent;
00101     _Base_ptr           _M_left;
00102     _Base_ptr           _M_right;
00103 
00104     static _Base_ptr
00105     _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00106     {
00107       while (__x->_M_left != 0) __x = __x->_M_left;
00108       return __x;
00109     }
00110 
00111     static _Const_Base_ptr
00112     _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00113     {
00114       while (__x->_M_left != 0) __x = __x->_M_left;
00115       return __x;
00116     }
00117 
00118     static _Base_ptr
00119     _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00120     {
00121       while (__x->_M_right != 0) __x = __x->_M_right;
00122       return __x;
00123     }
00124 
00125     static _Const_Base_ptr
00126     _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00127     {
00128       while (__x->_M_right != 0) __x = __x->_M_right;
00129       return __x;
00130     }
00131   };
00132 
00133   template<typename _Val>
00134     struct _Rb_tree_node : public _Rb_tree_node_base
00135     {
00136       typedef _Rb_tree_node<_Val>* _Link_type;
00137 
00138 #if __cplusplus < 201103L
00139       _Val _M_value_field;
00140 
00141       _Val*
00142       _M_valptr()
00143       { return std::__addressof(_M_value_field); }
00144 
00145       const _Val*
00146       _M_valptr() const
00147       { return std::__addressof(_M_value_field); }
00148 #else
00149       __gnu_cxx::__aligned_buffer<_Val> _M_storage;
00150 
00151       _Val*
00152       _M_valptr()
00153       { return _M_storage._M_ptr(); }
00154 
00155       const _Val*
00156       _M_valptr() const
00157       { return _M_storage._M_ptr(); }
00158 #endif
00159     };
00160 
00161   _GLIBCXX_PURE _Rb_tree_node_base*
00162   _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
00163 
00164   _GLIBCXX_PURE const _Rb_tree_node_base*
00165   _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
00166 
00167   _GLIBCXX_PURE _Rb_tree_node_base*
00168   _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
00169 
00170   _GLIBCXX_PURE const _Rb_tree_node_base*
00171   _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
00172 
00173   template<typename _Tp>
00174     struct _Rb_tree_iterator
00175     {
00176       typedef _Tp  value_type;
00177       typedef _Tp& reference;
00178       typedef _Tp* pointer;
00179 
00180       typedef bidirectional_iterator_tag iterator_category;
00181       typedef ptrdiff_t                  difference_type;
00182 
00183       typedef _Rb_tree_iterator<_Tp>        _Self;
00184       typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
00185       typedef _Rb_tree_node<_Tp>*           _Link_type;
00186 
00187       _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
00188       : _M_node() { }
00189 
00190       explicit
00191       _Rb_tree_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
00192       : _M_node(__x) { }
00193 
00194       reference
00195       operator*() const _GLIBCXX_NOEXCEPT
00196       { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
00197 
00198       pointer
00199       operator->() const _GLIBCXX_NOEXCEPT
00200       { return static_cast<_Link_type> (_M_node)->_M_valptr(); }
00201 
00202       _Self&
00203       operator++() _GLIBCXX_NOEXCEPT
00204       {
00205         _M_node = _Rb_tree_increment(_M_node);
00206         return *this;
00207       }
00208 
00209       _Self
00210       operator++(int) _GLIBCXX_NOEXCEPT
00211       {
00212         _Self __tmp = *this;
00213         _M_node = _Rb_tree_increment(_M_node);
00214         return __tmp;
00215       }
00216 
00217       _Self&
00218       operator--() _GLIBCXX_NOEXCEPT
00219       {
00220         _M_node = _Rb_tree_decrement(_M_node);
00221         return *this;
00222       }
00223 
00224       _Self
00225       operator--(int) _GLIBCXX_NOEXCEPT
00226       {
00227         _Self __tmp = *this;
00228         _M_node = _Rb_tree_decrement(_M_node);
00229         return __tmp;
00230       }
00231 
00232       bool
00233       operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
00234       { return _M_node == __x._M_node; }
00235 
00236       bool
00237       operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
00238       { return _M_node != __x._M_node; }
00239 
00240       _Base_ptr _M_node;
00241   };
00242 
00243   template<typename _Tp>
00244     struct _Rb_tree_const_iterator
00245     {
00246       typedef _Tp        value_type;
00247       typedef const _Tp& reference;
00248       typedef const _Tp* pointer;
00249 
00250       typedef _Rb_tree_iterator<_Tp> iterator;
00251 
00252       typedef bidirectional_iterator_tag iterator_category;
00253       typedef ptrdiff_t                  difference_type;
00254 
00255       typedef _Rb_tree_const_iterator<_Tp>        _Self;
00256       typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
00257       typedef const _Rb_tree_node<_Tp>*           _Link_type;
00258 
00259       _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
00260       : _M_node() { }
00261 
00262       explicit
00263       _Rb_tree_const_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
00264       : _M_node(__x) { }
00265 
00266       _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPT
00267       : _M_node(__it._M_node) { }
00268 
00269       iterator
00270       _M_const_cast() const _GLIBCXX_NOEXCEPT
00271       { return iterator(static_cast<typename iterator::_Link_type>
00272                         (const_cast<typename iterator::_Base_ptr>(_M_node))); }
00273 
00274       reference
00275       operator*() const _GLIBCXX_NOEXCEPT
00276       { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
00277 
00278       pointer
00279       operator->() const _GLIBCXX_NOEXCEPT
00280       { return static_cast<_Link_type>(_M_node)->_M_valptr(); }
00281 
00282       _Self&
00283       operator++() _GLIBCXX_NOEXCEPT
00284       {
00285         _M_node = _Rb_tree_increment(_M_node);
00286         return *this;
00287       }
00288 
00289       _Self
00290       operator++(int) _GLIBCXX_NOEXCEPT
00291       {
00292         _Self __tmp = *this;
00293         _M_node = _Rb_tree_increment(_M_node);
00294         return __tmp;
00295       }
00296 
00297       _Self&
00298       operator--() _GLIBCXX_NOEXCEPT
00299       {
00300         _M_node = _Rb_tree_decrement(_M_node);
00301         return *this;
00302       }
00303 
00304       _Self
00305       operator--(int) _GLIBCXX_NOEXCEPT
00306       {
00307         _Self __tmp = *this;
00308         _M_node = _Rb_tree_decrement(_M_node);
00309         return __tmp;
00310       }
00311 
00312       bool
00313       operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
00314       { return _M_node == __x._M_node; }
00315 
00316       bool
00317       operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
00318       { return _M_node != __x._M_node; }
00319 
00320       _Base_ptr _M_node;
00321     };
00322 
00323   template<typename _Val>
00324     inline bool
00325     operator==(const _Rb_tree_iterator<_Val>& __x,
00326                const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
00327     { return __x._M_node == __y._M_node; }
00328 
00329   template<typename _Val>
00330     inline bool
00331     operator!=(const _Rb_tree_iterator<_Val>& __x,
00332                const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
00333     { return __x._M_node != __y._M_node; }
00334 
00335   void
00336   _Rb_tree_insert_and_rebalance(const bool __insert_left,
00337                                 _Rb_tree_node_base* __x,
00338                                 _Rb_tree_node_base* __p,
00339                                 _Rb_tree_node_base& __header) throw ();
00340 
00341   _Rb_tree_node_base*
00342   _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
00343                                _Rb_tree_node_base& __header) throw ();
00344 
00345 
00346   template<typename _Key, typename _Val, typename _KeyOfValue,
00347            typename _Compare, typename _Alloc = allocator<_Val> >
00348     class _Rb_tree
00349     {
00350       typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
00351         rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
00352 
00353       typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
00354 
00355     protected:
00356       typedef _Rb_tree_node_base*               _Base_ptr;
00357       typedef const _Rb_tree_node_base*         _Const_Base_ptr;
00358       typedef _Rb_tree_node<_Val>*              _Link_type;
00359       typedef const _Rb_tree_node<_Val>*        _Const_Link_type;
00360 
00361     private:
00362       // Functor recycling a pool of nodes and using allocation once the pool
00363       // is empty.
00364       struct _Reuse_or_alloc_node
00365       {
00366         _Reuse_or_alloc_node(_Rb_tree& __t)
00367           : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
00368         {
00369           if (_M_root)
00370             {
00371               _M_root->_M_parent = 0;
00372 
00373               if (_M_nodes->_M_left)
00374                 _M_nodes = _M_nodes->_M_left;
00375             }
00376           else
00377             _M_nodes = 0;
00378         }
00379 
00380 #if __cplusplus >= 201103L
00381         _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
00382 #endif
00383 
00384         ~_Reuse_or_alloc_node()
00385         { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
00386 
00387         template<typename _Arg>
00388           _Link_type
00389 #if __cplusplus < 201103L
00390           operator()(const _Arg& __arg)
00391 #else
00392           operator()(_Arg&& __arg)
00393 #endif
00394           {
00395             _Link_type __node = static_cast<_Link_type>(_M_extract());
00396             if (__node)
00397               {
00398                 _M_t._M_destroy_node(__node);
00399                 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
00400                 return __node;
00401               }
00402 
00403             return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
00404           }
00405 
00406       private:
00407         _Base_ptr
00408         _M_extract()
00409         {
00410           if (!_M_nodes)
00411             return _M_nodes;
00412 
00413           _Base_ptr __node = _M_nodes;
00414           _M_nodes = _M_nodes->_M_parent;
00415           if (_M_nodes)
00416             {
00417               if (_M_nodes->_M_right == __node)
00418                 {
00419                   _M_nodes->_M_right = 0;
00420 
00421                   if (_M_nodes->_M_left)
00422                     {
00423                       _M_nodes = _M_nodes->_M_left;
00424 
00425                       while (_M_nodes->_M_right)
00426                         _M_nodes = _M_nodes->_M_right;
00427 
00428                       if (_M_nodes->_M_left)
00429                         _M_nodes = _M_nodes->_M_left;
00430                     }
00431                 }
00432               else // __node is on the left.
00433                 _M_nodes->_M_left = 0;
00434             }
00435           else
00436             _M_root = 0;
00437 
00438           return __node;
00439         }
00440 
00441         _Base_ptr _M_root;
00442         _Base_ptr _M_nodes;
00443         _Rb_tree& _M_t;
00444       };
00445 
00446       // Functor similar to the previous one but without any pool of nodes to
00447       // recycle.
00448       struct _Alloc_node
00449       {
00450         _Alloc_node(_Rb_tree& __t)
00451           : _M_t(__t) { }
00452 
00453         template<typename _Arg>
00454           _Link_type
00455 #if __cplusplus < 201103L
00456           operator()(const _Arg& __arg) const
00457 #else
00458           operator()(_Arg&& __arg) const
00459 #endif
00460           { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
00461 
00462       private:
00463         _Rb_tree& _M_t;
00464       };
00465 
00466     public:
00467       typedef _Key                              key_type;
00468       typedef _Val                              value_type;
00469       typedef value_type*                       pointer;
00470       typedef const value_type*                 const_pointer;
00471       typedef value_type&                       reference;
00472       typedef const value_type&                 const_reference;
00473       typedef size_t                            size_type;
00474       typedef ptrdiff_t                         difference_type;
00475       typedef _Alloc                            allocator_type;
00476 
00477       _Node_allocator&
00478       _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
00479       { return *static_cast<_Node_allocator*>(&this->_M_impl); }
00480       
00481       const _Node_allocator&
00482       _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
00483       { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
00484 
00485       allocator_type
00486       get_allocator() const _GLIBCXX_NOEXCEPT
00487       { return allocator_type(_M_get_Node_allocator()); }
00488 
00489     protected:
00490       _Link_type
00491       _M_get_node()
00492       { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
00493 
00494       void
00495       _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
00496       { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
00497 
00498 #if __cplusplus < 201103L
00499       void
00500       _M_construct_node(_Link_type __node, const value_type& __x)
00501       {
00502         __try
00503           { get_allocator().construct(__node->_M_valptr(), __x); }
00504         __catch(...)
00505           {
00506             _M_put_node(__node);
00507             __throw_exception_again;
00508           }
00509       }
00510 
00511       _Link_type
00512       _M_create_node(const value_type& __x)
00513       {
00514         _Link_type __tmp = _M_get_node();
00515         _M_construct_node(__tmp, __x);
00516         return __tmp;
00517       }
00518 
00519       void
00520       _M_destroy_node(_Link_type __p)
00521       { get_allocator().destroy(__p->_M_valptr()); }
00522 #else
00523       template<typename... _Args>
00524         void
00525         _M_construct_node(_Link_type __node, _Args&&... __args)
00526         {
00527           __try
00528             {
00529               ::new(__node) _Rb_tree_node<_Val>;
00530               _Alloc_traits::construct(_M_get_Node_allocator(),
00531                                        __node->_M_valptr(),
00532                                        std::forward<_Args>(__args)...);
00533             }
00534           __catch(...)
00535             {
00536               __node->~_Rb_tree_node<_Val>();
00537               _M_put_node(__node);
00538               __throw_exception_again;
00539             }
00540         }
00541 
00542       template<typename... _Args>
00543         _Link_type
00544         _M_create_node(_Args&&... __args)
00545         {
00546           _Link_type __tmp = _M_get_node();
00547           _M_construct_node(__tmp, std::forward<_Args>(__args)...);
00548           return __tmp;
00549         }
00550 
00551       void
00552       _M_destroy_node(_Link_type __p) noexcept
00553       {
00554         _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
00555         __p->~_Rb_tree_node<_Val>();
00556       }
00557 #endif
00558 
00559       void
00560       _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
00561       {
00562         _M_destroy_node(__p);
00563         _M_put_node(__p);
00564       }
00565 
00566       template<typename _NodeGen>
00567         _Link_type
00568         _M_clone_node(_Const_Link_type __x, _NodeGen& __node_gen)
00569         {
00570           _Link_type __tmp = __node_gen(*__x->_M_valptr());
00571           __tmp->_M_color = __x->_M_color;
00572           __tmp->_M_left = 0;
00573           __tmp->_M_right = 0;
00574           return __tmp;
00575         }
00576 
00577     protected:
00578       // Unused _Is_pod_comparator is kept as it is part of mangled name.
00579       template<typename _Key_compare,
00580                bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
00581         struct _Rb_tree_impl : public _Node_allocator
00582         {
00583           _Key_compare          _M_key_compare;
00584           _Rb_tree_node_base    _M_header;
00585           size_type             _M_node_count; // Keeps track of size of tree.
00586 
00587           _Rb_tree_impl()
00588           : _Node_allocator(), _M_key_compare(), _M_header(),
00589             _M_node_count(0)
00590           { _M_initialize(); }
00591 
00592           _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
00593           : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
00594             _M_node_count(0)
00595           { _M_initialize(); }
00596 
00597 #if __cplusplus >= 201103L
00598           _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
00599           : _Node_allocator(std::move(__a)), _M_key_compare(__comp),
00600             _M_header(), _M_node_count(0)
00601           { _M_initialize(); }
00602 #endif
00603 
00604           void
00605           _M_reset()
00606           {
00607             this->_M_header._M_parent = 0;
00608             this->_M_header._M_left = &this->_M_header;
00609             this->_M_header._M_right = &this->_M_header;
00610             this->_M_node_count = 0;
00611           }
00612 
00613         private:
00614           void
00615           _M_initialize()
00616           {
00617             this->_M_header._M_color = _S_red;
00618             this->_M_header._M_parent = 0;
00619             this->_M_header._M_left = &this->_M_header;
00620             this->_M_header._M_right = &this->_M_header;
00621           }         
00622         };
00623 
00624       _Rb_tree_impl<_Compare> _M_impl;
00625 
00626     protected:
00627       _Base_ptr&
00628       _M_root() _GLIBCXX_NOEXCEPT
00629       { return this->_M_impl._M_header._M_parent; }
00630 
00631       _Const_Base_ptr
00632       _M_root() const _GLIBCXX_NOEXCEPT
00633       { return this->_M_impl._M_header._M_parent; }
00634 
00635       _Base_ptr&
00636       _M_leftmost() _GLIBCXX_NOEXCEPT
00637       { return this->_M_impl._M_header._M_left; }
00638 
00639       _Const_Base_ptr
00640       _M_leftmost() const _GLIBCXX_NOEXCEPT
00641       { return this->_M_impl._M_header._M_left; }
00642 
00643       _Base_ptr&
00644       _M_rightmost() _GLIBCXX_NOEXCEPT
00645       { return this->_M_impl._M_header._M_right; }
00646 
00647       _Const_Base_ptr
00648       _M_rightmost() const _GLIBCXX_NOEXCEPT
00649       { return this->_M_impl._M_header._M_right; }
00650 
00651       _Link_type
00652       _M_begin() _GLIBCXX_NOEXCEPT
00653       { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
00654 
00655       _Const_Link_type
00656       _M_begin() const _GLIBCXX_NOEXCEPT
00657       {
00658         return static_cast<_Const_Link_type>
00659           (this->_M_impl._M_header._M_parent);
00660       }
00661 
00662       _Link_type
00663       _M_end() _GLIBCXX_NOEXCEPT
00664       { return reinterpret_cast<_Link_type>(&this->_M_impl._M_header); }
00665 
00666       _Const_Link_type
00667       _M_end() const _GLIBCXX_NOEXCEPT
00668       { return reinterpret_cast<_Const_Link_type>(&this->_M_impl._M_header); }
00669 
00670       static const_reference
00671       _S_value(_Const_Link_type __x)
00672       { return *__x->_M_valptr(); }
00673 
00674       static const _Key&
00675       _S_key(_Const_Link_type __x)
00676       { return _KeyOfValue()(_S_value(__x)); }
00677 
00678       static _Link_type
00679       _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00680       { return static_cast<_Link_type>(__x->_M_left); }
00681 
00682       static _Const_Link_type
00683       _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00684       { return static_cast<_Const_Link_type>(__x->_M_left); }
00685 
00686       static _Link_type
00687       _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00688       { return static_cast<_Link_type>(__x->_M_right); }
00689 
00690       static _Const_Link_type
00691       _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00692       { return static_cast<_Const_Link_type>(__x->_M_right); }
00693 
00694       static const_reference
00695       _S_value(_Const_Base_ptr __x)
00696       { return *static_cast<_Const_Link_type>(__x)->_M_valptr(); }
00697 
00698       static const _Key&
00699       _S_key(_Const_Base_ptr __x)
00700       { return _KeyOfValue()(_S_value(__x)); }
00701 
00702       static _Base_ptr
00703       _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00704       { return _Rb_tree_node_base::_S_minimum(__x); }
00705 
00706       static _Const_Base_ptr
00707       _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00708       { return _Rb_tree_node_base::_S_minimum(__x); }
00709 
00710       static _Base_ptr
00711       _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
00712       { return _Rb_tree_node_base::_S_maximum(__x); }
00713 
00714       static _Const_Base_ptr
00715       _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
00716       { return _Rb_tree_node_base::_S_maximum(__x); }
00717 
00718     public:
00719       typedef _Rb_tree_iterator<value_type>       iterator;
00720       typedef _Rb_tree_const_iterator<value_type> const_iterator;
00721 
00722       typedef std::reverse_iterator<iterator>       reverse_iterator;
00723       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00724 
00725     private:
00726       pair<_Base_ptr, _Base_ptr>
00727       _M_get_insert_unique_pos(const key_type& __k);
00728 
00729       pair<_Base_ptr, _Base_ptr>
00730       _M_get_insert_equal_pos(const key_type& __k);
00731 
00732       pair<_Base_ptr, _Base_ptr>
00733       _M_get_insert_hint_unique_pos(const_iterator __pos,
00734                                     const key_type& __k);
00735 
00736       pair<_Base_ptr, _Base_ptr>
00737       _M_get_insert_hint_equal_pos(const_iterator __pos,
00738                                    const key_type& __k);
00739 
00740 #if __cplusplus >= 201103L
00741       template<typename _Arg, typename _NodeGen>
00742         iterator
00743         _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
00744 
00745       iterator
00746       _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
00747 
00748       template<typename _Arg>
00749         iterator
00750         _M_insert_lower(_Base_ptr __y, _Arg&& __v);
00751 
00752       template<typename _Arg>
00753         iterator
00754         _M_insert_equal_lower(_Arg&& __x);
00755 
00756       iterator
00757       _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
00758 
00759       iterator
00760       _M_insert_equal_lower_node(_Link_type __z);
00761 #else
00762       template<typename _NodeGen>
00763         iterator
00764         _M_insert_(_Base_ptr __x, _Base_ptr __y,
00765                    const value_type& __v, _NodeGen&);
00766 
00767       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00768       // 233. Insertion hints in associative containers.
00769       iterator
00770       _M_insert_lower(_Base_ptr __y, const value_type& __v);
00771 
00772       iterator
00773       _M_insert_equal_lower(const value_type& __x);
00774 #endif
00775 
00776       template<typename _NodeGen>
00777         _Link_type
00778         _M_copy(_Const_Link_type __x, _Link_type __p, _NodeGen&);
00779 
00780       _Link_type
00781       _M_copy(_Const_Link_type __x, _Link_type __p)
00782       {
00783         _Alloc_node __an(*this);
00784         return _M_copy(__x, __p, __an);
00785       }
00786 
00787       void
00788       _M_erase(_Link_type __x);
00789 
00790       iterator
00791       _M_lower_bound(_Link_type __x, _Link_type __y,
00792                      const _Key& __k);
00793 
00794       const_iterator
00795       _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
00796                      const _Key& __k) const;
00797 
00798       iterator
00799       _M_upper_bound(_Link_type __x, _Link_type __y,
00800                      const _Key& __k);
00801 
00802       const_iterator
00803       _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
00804                      const _Key& __k) const;
00805 
00806     public:
00807       // allocation/deallocation
00808       _Rb_tree() { }
00809 
00810       _Rb_tree(const _Compare& __comp,
00811                const allocator_type& __a = allocator_type())
00812       : _M_impl(__comp, _Node_allocator(__a)) { }
00813 
00814       _Rb_tree(const _Rb_tree& __x)
00815       : _M_impl(__x._M_impl._M_key_compare,
00816                 _Alloc_traits::_S_select_on_copy(__x._M_get_Node_allocator()))
00817       {
00818         if (__x._M_root() != 0)
00819           {
00820             _M_root() = _M_copy(__x._M_begin(), _M_end());
00821             _M_leftmost() = _S_minimum(_M_root());
00822             _M_rightmost() = _S_maximum(_M_root());
00823             _M_impl._M_node_count = __x._M_impl._M_node_count;
00824           }
00825       }
00826 
00827 #if __cplusplus >= 201103L
00828       _Rb_tree(const allocator_type& __a)
00829       : _M_impl(_Compare(), _Node_allocator(__a))
00830       { }
00831 
00832       _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
00833       : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
00834       {
00835         if (__x._M_root() != nullptr)
00836           {
00837             _M_root() = _M_copy(__x._M_begin(), _M_end());
00838             _M_leftmost() = _S_minimum(_M_root());
00839             _M_rightmost() = _S_maximum(_M_root());
00840             _M_impl._M_node_count = __x._M_impl._M_node_count;
00841           }
00842       }
00843 
00844       _Rb_tree(_Rb_tree&& __x)
00845       : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
00846       {
00847         if (__x._M_root() != 0)
00848           _M_move_data(__x, std::true_type());
00849       }
00850 
00851       _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
00852       : _Rb_tree(std::move(__x), _Node_allocator(__a))
00853       { }
00854 
00855       _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a);
00856 #endif
00857 
00858       ~_Rb_tree() _GLIBCXX_NOEXCEPT
00859       { _M_erase(_M_begin()); }
00860 
00861       _Rb_tree&
00862       operator=(const _Rb_tree& __x);
00863 
00864       // Accessors.
00865       _Compare
00866       key_comp() const
00867       { return _M_impl._M_key_compare; }
00868 
00869       iterator
00870       begin() _GLIBCXX_NOEXCEPT
00871       { 
00872         return iterator(static_cast<_Link_type>
00873                         (this->_M_impl._M_header._M_left));
00874       }
00875 
00876       const_iterator
00877       begin() const _GLIBCXX_NOEXCEPT
00878       { 
00879         return const_iterator(static_cast<_Const_Link_type>
00880                               (this->_M_impl._M_header._M_left));
00881       }
00882 
00883       iterator
00884       end() _GLIBCXX_NOEXCEPT
00885       { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
00886 
00887       const_iterator
00888       end() const _GLIBCXX_NOEXCEPT
00889       { 
00890         return const_iterator(static_cast<_Const_Link_type>
00891                               (&this->_M_impl._M_header));
00892       }
00893 
00894       reverse_iterator
00895       rbegin() _GLIBCXX_NOEXCEPT
00896       { return reverse_iterator(end()); }
00897 
00898       const_reverse_iterator
00899       rbegin() const _GLIBCXX_NOEXCEPT
00900       { return const_reverse_iterator(end()); }
00901 
00902       reverse_iterator
00903       rend() _GLIBCXX_NOEXCEPT
00904       { return reverse_iterator(begin()); }
00905 
00906       const_reverse_iterator
00907       rend() const _GLIBCXX_NOEXCEPT
00908       { return const_reverse_iterator(begin()); }
00909 
00910       bool
00911       empty() const _GLIBCXX_NOEXCEPT
00912       { return _M_impl._M_node_count == 0; }
00913 
00914       size_type
00915       size() const _GLIBCXX_NOEXCEPT 
00916       { return _M_impl._M_node_count; }
00917 
00918       size_type
00919       max_size() const _GLIBCXX_NOEXCEPT
00920       { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
00921 
00922       void
00923 #if __cplusplus >= 201103L
00924       swap(_Rb_tree& __t) noexcept(_Alloc_traits::_S_nothrow_swap());
00925 #else
00926       swap(_Rb_tree& __t);
00927 #endif
00928 
00929       // Insert/erase.
00930 #if __cplusplus >= 201103L
00931       template<typename _Arg>
00932         pair<iterator, bool>
00933         _M_insert_unique(_Arg&& __x);
00934 
00935       template<typename _Arg>
00936         iterator
00937         _M_insert_equal(_Arg&& __x);
00938 
00939       template<typename _Arg, typename _NodeGen>
00940         iterator
00941         _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
00942 
00943       template<typename _Arg>
00944         iterator
00945         _M_insert_unique_(const_iterator __pos, _Arg&& __x)
00946         {
00947           _Alloc_node __an(*this);
00948           return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
00949         }
00950 
00951       template<typename _Arg, typename _NodeGen>
00952         iterator
00953         _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
00954 
00955       template<typename _Arg>
00956         iterator
00957         _M_insert_equal_(const_iterator __pos, _Arg&& __x)
00958         {
00959           _Alloc_node __an(*this);
00960           return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
00961         }
00962 
00963       template<typename... _Args>
00964         pair<iterator, bool>
00965         _M_emplace_unique(_Args&&... __args);
00966 
00967       template<typename... _Args>
00968         iterator
00969         _M_emplace_equal(_Args&&... __args);
00970 
00971       template<typename... _Args>
00972         iterator
00973         _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
00974 
00975       template<typename... _Args>
00976         iterator
00977         _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
00978 #else
00979       pair<iterator, bool>
00980       _M_insert_unique(const value_type& __x);
00981 
00982       iterator
00983       _M_insert_equal(const value_type& __x);
00984 
00985       template<typename _NodeGen>
00986         iterator
00987         _M_insert_unique_(const_iterator __pos, const value_type& __x,
00988                           _NodeGen&);
00989 
00990       iterator
00991       _M_insert_unique_(const_iterator __pos, const value_type& __x)
00992       {
00993         _Alloc_node __an(*this);
00994         return _M_insert_unique_(__pos, __x, __an);
00995       }
00996 
00997       template<typename _NodeGen>
00998         iterator
00999         _M_insert_equal_(const_iterator __pos, const value_type& __x,
01000                          _NodeGen&);
01001       iterator
01002       _M_insert_equal_(const_iterator __pos, const value_type& __x)
01003       {
01004         _Alloc_node __an(*this);
01005         return _M_insert_equal_(__pos, __x, __an);
01006       }
01007 #endif
01008 
01009       template<typename _InputIterator>
01010         void
01011         _M_insert_unique(_InputIterator __first, _InputIterator __last);
01012 
01013       template<typename _InputIterator>
01014         void
01015         _M_insert_equal(_InputIterator __first, _InputIterator __last);
01016 
01017     private:
01018       void
01019       _M_erase_aux(const_iterator __position);
01020 
01021       void
01022       _M_erase_aux(const_iterator __first, const_iterator __last);
01023 
01024     public:
01025 #if __cplusplus >= 201103L
01026       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01027       // DR 130. Associative erase should return an iterator.
01028       _GLIBCXX_ABI_TAG_CXX11
01029       iterator
01030       erase(const_iterator __position)
01031       {
01032         const_iterator __result = __position;
01033         ++__result;
01034         _M_erase_aux(__position);
01035         return __result._M_const_cast();
01036       }
01037 
01038       // LWG 2059.
01039       _GLIBCXX_ABI_TAG_CXX11
01040       iterator
01041       erase(iterator __position)
01042       {
01043         iterator __result = __position;
01044         ++__result;
01045         _M_erase_aux(__position);
01046         return __result;
01047       }
01048 #else
01049       void
01050       erase(iterator __position)
01051       { _M_erase_aux(__position); }
01052 
01053       void
01054       erase(const_iterator __position)
01055       { _M_erase_aux(__position); }
01056 #endif
01057       size_type
01058       erase(const key_type& __x);
01059 
01060 #if __cplusplus >= 201103L
01061       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01062       // DR 130. Associative erase should return an iterator.
01063       _GLIBCXX_ABI_TAG_CXX11
01064       iterator
01065       erase(const_iterator __first, const_iterator __last)
01066       {
01067         _M_erase_aux(__first, __last);
01068         return __last._M_const_cast();
01069       }
01070 #else
01071       void
01072       erase(iterator __first, iterator __last)
01073       { _M_erase_aux(__first, __last); }
01074 
01075       void
01076       erase(const_iterator __first, const_iterator __last)
01077       { _M_erase_aux(__first, __last); }
01078 #endif
01079       void
01080       erase(const key_type* __first, const key_type* __last);
01081 
01082       void
01083       clear() _GLIBCXX_NOEXCEPT
01084       {
01085         _M_erase(_M_begin());
01086         _M_impl._M_reset();
01087       }
01088 
01089       // Set operations.
01090       iterator
01091       find(const key_type& __k);
01092 
01093       const_iterator
01094       find(const key_type& __k) const;
01095 
01096       size_type
01097       count(const key_type& __k) const;
01098 
01099       iterator
01100       lower_bound(const key_type& __k)
01101       { return _M_lower_bound(_M_begin(), _M_end(), __k); }
01102 
01103       const_iterator
01104       lower_bound(const key_type& __k) const
01105       { return _M_lower_bound(_M_begin(), _M_end(), __k); }
01106 
01107       iterator
01108       upper_bound(const key_type& __k)
01109       { return _M_upper_bound(_M_begin(), _M_end(), __k); }
01110 
01111       const_iterator
01112       upper_bound(const key_type& __k) const
01113       { return _M_upper_bound(_M_begin(), _M_end(), __k); }
01114 
01115       pair<iterator, iterator>
01116       equal_range(const key_type& __k);
01117 
01118       pair<const_iterator, const_iterator>
01119       equal_range(const key_type& __k) const;
01120 
01121 #if __cplusplus > 201103L
01122       template<typename _Cmp, typename _Kt, typename = __void_t<>>
01123         struct __is_transparent { };
01124 
01125       template<typename _Cmp, typename _Kt>
01126         struct
01127         __is_transparent<_Cmp, _Kt, __void_t<typename _Cmp::is_transparent>>
01128         { typedef void type; };
01129 
01130       static auto _S_iter(_Link_type __x) { return iterator(__x); }
01131 
01132       static auto _S_iter(_Const_Link_type __x) { return const_iterator(__x); }
01133 
01134       template<typename _Cmp, typename _Link, typename _Kt>
01135         static auto
01136         _S_lower_bound_tr(_Cmp& __cmp, _Link __x, _Link __y, const _Kt& __k)
01137         {
01138           while (__x != 0)
01139             if (!__cmp(_S_key(__x), __k))
01140               __y = __x, __x = _S_left(__x);
01141             else
01142               __x = _S_right(__x);
01143           return _S_iter(__y);
01144         }
01145 
01146       template<typename _Cmp, typename _Link, typename _Kt>
01147         static auto
01148         _S_upper_bound_tr(_Cmp& __cmp, _Link __x, _Link __y, const _Kt& __k)
01149         {
01150           while (__x != 0)
01151             if (__cmp(__k, _S_key(__x)))
01152               __y = __x, __x = _S_left(__x);
01153             else
01154               __x = _S_right(__x);
01155           return _S_iter(__y);
01156         }
01157 
01158       template<typename _Kt,
01159                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01160         iterator
01161         _M_find_tr(const _Kt& __k)
01162         {
01163           auto& __cmp = _M_impl._M_key_compare;
01164           auto __j = _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01165           return (__j == end() || __cmp(__k, _S_key(__j._M_node)))
01166             ? end() : __j;
01167         }
01168 
01169       template<typename _Kt,
01170                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01171         const_iterator
01172         _M_find_tr(const _Kt& __k) const
01173         {
01174           auto& __cmp = _M_impl._M_key_compare;
01175           auto __j = _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01176           return (__j == end() || __cmp(__k, _S_key(__j._M_node)))
01177             ? end() : __j;
01178         }
01179 
01180       template<typename _Kt,
01181                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01182         size_type
01183         _M_count_tr(const _Kt& __k) const
01184         {
01185           auto __p = _M_equal_range_tr(__k);
01186           return std::distance(__p.first, __p.second);
01187         }
01188 
01189       template<typename _Kt,
01190                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01191         iterator
01192         _M_lower_bound_tr(const _Kt& __k)
01193         {
01194           auto& __cmp = _M_impl._M_key_compare;
01195           return _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01196         }
01197 
01198       template<typename _Kt,
01199                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01200         const_iterator
01201         _M_lower_bound_tr(const _Kt& __k) const
01202         {
01203           auto& __cmp = _M_impl._M_key_compare;
01204           return _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01205         }
01206 
01207       template<typename _Kt,
01208                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01209         iterator
01210         _M_upper_bound_tr(const _Kt& __k)
01211         {
01212           auto& __cmp = _M_impl._M_key_compare;
01213           return _S_upper_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01214         }
01215 
01216       template<typename _Kt,
01217                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01218         const_iterator
01219         _M_upper_bound_tr(const _Kt& __k) const
01220         {
01221           auto& __cmp = _M_impl._M_key_compare;
01222           return _S_upper_bound_tr(__cmp, _M_begin(), _M_end(), __k);
01223         }
01224 
01225       template<typename _Kt,
01226                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01227         pair<iterator, iterator>
01228         _M_equal_range_tr(const _Kt& __k)
01229         {
01230           auto __low = _M_lower_bound_tr(__k);
01231           auto __high = __low;
01232           auto& __cmp = _M_impl._M_key_compare;
01233           while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
01234             ++__high;
01235           return { __low, __high };
01236         }
01237 
01238       template<typename _Kt,
01239                typename _Req = typename __is_transparent<_Compare, _Kt>::type>
01240         pair<const_iterator, const_iterator>
01241         _M_equal_range_tr(const _Kt& __k) const
01242         {
01243           auto __low = _M_lower_bound_tr(__k);
01244           auto __high = __low;
01245           auto& __cmp = _M_impl._M_key_compare;
01246           while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
01247             ++__high;
01248           return { __low, __high };
01249         }
01250 #endif
01251 
01252       // Debugging.
01253       bool
01254       __rb_verify() const;
01255 
01256 #if __cplusplus >= 201103L
01257       _Rb_tree&
01258       operator=(_Rb_tree&&) noexcept(_Alloc_traits::_S_nothrow_move());
01259 
01260       template<typename _Iterator>
01261         void
01262         _M_assign_unique(_Iterator, _Iterator);
01263 
01264       template<typename _Iterator>
01265         void
01266         _M_assign_equal(_Iterator, _Iterator);
01267 
01268     private:
01269       // Move elements from container with equal allocator.
01270       void
01271       _M_move_data(_Rb_tree&, std::true_type);
01272 
01273       // Move elements from container with possibly non-equal allocator,
01274       // which might result in a copy not a move.
01275       void
01276       _M_move_data(_Rb_tree&, std::false_type);
01277 #endif
01278     };
01279 
01280   template<typename _Key, typename _Val, typename _KeyOfValue,
01281            typename _Compare, typename _Alloc>
01282     inline bool
01283     operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01284                const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01285     {
01286       return __x.size() == __y.size()
01287              && std::equal(__x.begin(), __x.end(), __y.begin());
01288     }
01289 
01290   template<typename _Key, typename _Val, typename _KeyOfValue,
01291            typename _Compare, typename _Alloc>
01292     inline bool
01293     operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01294               const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01295     {
01296       return std::lexicographical_compare(__x.begin(), __x.end(), 
01297                                           __y.begin(), __y.end());
01298     }
01299 
01300   template<typename _Key, typename _Val, typename _KeyOfValue,
01301            typename _Compare, typename _Alloc>
01302     inline bool
01303     operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01304                const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01305     { return !(__x == __y); }
01306 
01307   template<typename _Key, typename _Val, typename _KeyOfValue,
01308            typename _Compare, typename _Alloc>
01309     inline bool
01310     operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01311               const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01312     { return __y < __x; }
01313 
01314   template<typename _Key, typename _Val, typename _KeyOfValue,
01315            typename _Compare, typename _Alloc>
01316     inline bool
01317     operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01318                const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01319     { return !(__y < __x); }
01320 
01321   template<typename _Key, typename _Val, typename _KeyOfValue,
01322            typename _Compare, typename _Alloc>
01323     inline bool
01324     operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01325                const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01326     { return !(__x < __y); }
01327 
01328   template<typename _Key, typename _Val, typename _KeyOfValue,
01329            typename _Compare, typename _Alloc>
01330     inline void
01331     swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
01332          _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
01333     { __x.swap(__y); }
01334 
01335 #if __cplusplus >= 201103L
01336   template<typename _Key, typename _Val, typename _KeyOfValue,
01337            typename _Compare, typename _Alloc>
01338     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01339     _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
01340     : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
01341     {
01342       using __eq = integral_constant<bool, _Alloc_traits::_S_always_equal()>;
01343       if (__x._M_root() != nullptr)
01344         _M_move_data(__x, __eq());
01345     }
01346 
01347   template<typename _Key, typename _Val, typename _KeyOfValue,
01348            typename _Compare, typename _Alloc>
01349     void
01350     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01351     _M_move_data(_Rb_tree& __x, std::true_type)
01352     {
01353       _M_root() = __x._M_root();
01354       _M_leftmost() = __x._M_leftmost();
01355       _M_rightmost() = __x._M_rightmost();
01356       _M_root()->_M_parent = _M_end();
01357 
01358       __x._M_root() = 0;
01359       __x._M_leftmost() = __x._M_end();
01360       __x._M_rightmost() = __x._M_end();
01361 
01362       this->_M_impl._M_node_count = __x._M_impl._M_node_count;
01363       __x._M_impl._M_node_count = 0;
01364     }
01365 
01366   template<typename _Key, typename _Val, typename _KeyOfValue,
01367            typename _Compare, typename _Alloc>
01368     void
01369     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01370     _M_move_data(_Rb_tree& __x, std::false_type)
01371     {
01372       if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
01373           _M_move_data(__x, std::true_type());
01374       else
01375         {
01376           _Alloc_node __an(*this);
01377           auto __lbd =
01378             [&__an](const value_type& __cval)
01379             {
01380               auto& __val = const_cast<value_type&>(__cval);
01381               return __an(std::move_if_noexcept(__val));
01382             };
01383           _M_root() = _M_copy(__x._M_begin(), _M_end(), __lbd);
01384           _M_leftmost() = _S_minimum(_M_root());
01385           _M_rightmost() = _S_maximum(_M_root());
01386           _M_impl._M_node_count = __x._M_impl._M_node_count;
01387         }
01388     }
01389 
01390   template<typename _Key, typename _Val, typename _KeyOfValue,
01391            typename _Compare, typename _Alloc>
01392     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
01393     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01394     operator=(_Rb_tree&& __x)
01395     noexcept(_Alloc_traits::_S_nothrow_move())
01396     {
01397       _M_impl._M_key_compare = __x._M_impl._M_key_compare;
01398       if (_Alloc_traits::_S_propagate_on_move_assign()
01399           || _Alloc_traits::_S_always_equal()
01400           || _M_get_Node_allocator() == __x._M_get_Node_allocator())
01401         {
01402           clear();
01403           if (__x._M_root() != nullptr)
01404             _M_move_data(__x, std::true_type());
01405           std::__alloc_on_move(_M_get_Node_allocator(),
01406                                __x._M_get_Node_allocator());
01407           return *this;
01408         }
01409 
01410       // Try to move each node reusing existing nodes and copying __x nodes
01411       // structure.
01412       _Reuse_or_alloc_node __roan(*this);
01413       _M_impl._M_reset();
01414       if (__x._M_root() != nullptr)
01415         {
01416           auto __lbd =
01417             [&__roan](const value_type& __cval)
01418             {
01419               auto& __val = const_cast<value_type&>(__cval);
01420               return __roan(std::move_if_noexcept(__val));
01421             };
01422           _M_root() = _M_copy(__x._M_begin(), _M_end(), __lbd);
01423           _M_leftmost() = _S_minimum(_M_root());
01424           _M_rightmost() = _S_maximum(_M_root());
01425           _M_impl._M_node_count = __x._M_impl._M_node_count;
01426           __x.clear();
01427         }
01428       return *this;
01429     }
01430 
01431   template<typename _Key, typename _Val, typename _KeyOfValue,
01432            typename _Compare, typename _Alloc>
01433     template<typename _Iterator>
01434       void
01435       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01436       _M_assign_unique(_Iterator __first, _Iterator __last)
01437       {
01438         _Reuse_or_alloc_node __roan(*this);
01439         _M_impl._M_reset();
01440         for (; __first != __last; ++__first)
01441           _M_insert_unique_(end(), *__first, __roan);
01442       }
01443 
01444   template<typename _Key, typename _Val, typename _KeyOfValue,
01445            typename _Compare, typename _Alloc>
01446     template<typename _Iterator>
01447       void
01448       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01449       _M_assign_equal(_Iterator __first, _Iterator __last)
01450       {
01451         _Reuse_or_alloc_node __roan(*this);
01452         _M_impl._M_reset();
01453         for (; __first != __last; ++__first)
01454           _M_insert_equal_(end(), *__first, __roan);
01455       }
01456 #endif
01457 
01458   template<typename _Key, typename _Val, typename _KeyOfValue,
01459            typename _Compare, typename _Alloc>
01460     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
01461     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01462     operator=(const _Rb_tree& __x)
01463     {
01464       if (this != &__x)
01465         {
01466           // Note that _Key may be a constant type.
01467 #if __cplusplus >= 201103L
01468           if (_Alloc_traits::_S_propagate_on_copy_assign())
01469             {
01470               auto& __this_alloc = this->_M_get_Node_allocator();
01471               auto& __that_alloc = __x._M_get_Node_allocator();
01472               if (!_Alloc_traits::_S_always_equal()
01473                   && __this_alloc != __that_alloc)
01474                 {
01475                   // Replacement allocator cannot free existing storage, we need
01476                   // to erase nodes first.
01477                   clear();
01478                   std::__alloc_on_copy(__this_alloc, __that_alloc);
01479                 }
01480             }
01481 #endif
01482 
01483           _Reuse_or_alloc_node __roan(*this);
01484           _M_impl._M_reset();
01485           _M_impl._M_key_compare = __x._M_impl._M_key_compare;
01486           if (__x._M_root() != 0)
01487             {
01488               _M_root() = _M_copy(__x._M_begin(), _M_end(), __roan);
01489               _M_leftmost() = _S_minimum(_M_root());
01490               _M_rightmost() = _S_maximum(_M_root());
01491               _M_impl._M_node_count = __x._M_impl._M_node_count;
01492             }
01493         }
01494 
01495       return *this;
01496     }
01497 
01498   template<typename _Key, typename _Val, typename _KeyOfValue,
01499            typename _Compare, typename _Alloc>
01500 #if __cplusplus >= 201103L
01501     template<typename _Arg, typename _NodeGen>
01502 #else
01503     template<typename _NodeGen>
01504 #endif
01505       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01506       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01507       _M_insert_(_Base_ptr __x, _Base_ptr __p,
01508 #if __cplusplus >= 201103L
01509                  _Arg&& __v,
01510 #else
01511                  const _Val& __v,
01512 #endif
01513                  _NodeGen& __node_gen)
01514       {
01515         bool __insert_left = (__x != 0 || __p == _M_end()
01516                               || _M_impl._M_key_compare(_KeyOfValue()(__v),
01517                                                         _S_key(__p)));
01518 
01519         _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
01520 
01521         _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
01522                                       this->_M_impl._M_header);
01523         ++_M_impl._M_node_count;
01524         return iterator(__z);
01525       }
01526 
01527   template<typename _Key, typename _Val, typename _KeyOfValue,
01528            typename _Compare, typename _Alloc>
01529 #if __cplusplus >= 201103L
01530     template<typename _Arg>
01531 #endif
01532     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01533     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01534 #if __cplusplus >= 201103L
01535     _M_insert_lower(_Base_ptr __p, _Arg&& __v)
01536 #else
01537     _M_insert_lower(_Base_ptr __p, const _Val& __v)
01538 #endif
01539     {
01540       bool __insert_left = (__p == _M_end()
01541                             || !_M_impl._M_key_compare(_S_key(__p),
01542                                                        _KeyOfValue()(__v)));
01543 
01544       _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
01545 
01546       _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
01547                                     this->_M_impl._M_header);
01548       ++_M_impl._M_node_count;
01549       return iterator(__z);
01550     }
01551 
01552   template<typename _Key, typename _Val, typename _KeyOfValue,
01553            typename _Compare, typename _Alloc>
01554 #if __cplusplus >= 201103L
01555     template<typename _Arg>
01556 #endif
01557     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01558     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01559 #if __cplusplus >= 201103L
01560     _M_insert_equal_lower(_Arg&& __v)
01561 #else
01562     _M_insert_equal_lower(const _Val& __v)
01563 #endif
01564     {
01565       _Link_type __x = _M_begin();
01566       _Link_type __y = _M_end();
01567       while (__x != 0)
01568         {
01569           __y = __x;
01570           __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
01571                 _S_left(__x) : _S_right(__x);
01572         }
01573       return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
01574     }
01575 
01576   template<typename _Key, typename _Val, typename _KoV,
01577            typename _Compare, typename _Alloc>
01578     template<typename _NodeGen>
01579       typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
01580       _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
01581       _M_copy(_Const_Link_type __x, _Link_type __p, _NodeGen& __node_gen)
01582       {
01583         // Structural copy. __x and __p must be non-null.
01584         _Link_type __top = _M_clone_node(__x, __node_gen);
01585         __top->_M_parent = __p;
01586 
01587         __try
01588           {
01589             if (__x->_M_right)
01590               __top->_M_right = _M_copy(_S_right(__x), __top, __node_gen);
01591             __p = __top;
01592             __x = _S_left(__x);
01593 
01594             while (__x != 0)
01595               {
01596                 _Link_type __y = _M_clone_node(__x, __node_gen);
01597                 __p->_M_left = __y;
01598                 __y->_M_parent = __p;
01599                 if (__x->_M_right)
01600                   __y->_M_right = _M_copy(_S_right(__x), __y, __node_gen);
01601                 __p = __y;
01602                 __x = _S_left(__x);
01603               }
01604           }
01605         __catch(...)
01606           {
01607             _M_erase(__top);
01608             __throw_exception_again;
01609           }
01610         return __top;
01611       }
01612 
01613   template<typename _Key, typename _Val, typename _KeyOfValue,
01614            typename _Compare, typename _Alloc>
01615     void
01616     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01617     _M_erase(_Link_type __x)
01618     {
01619       // Erase without rebalancing.
01620       while (__x != 0)
01621         {
01622           _M_erase(_S_right(__x));
01623           _Link_type __y = _S_left(__x);
01624           _M_drop_node(__x);
01625           __x = __y;
01626         }
01627     }
01628 
01629   template<typename _Key, typename _Val, typename _KeyOfValue,
01630            typename _Compare, typename _Alloc>
01631     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01632                       _Compare, _Alloc>::iterator
01633     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01634     _M_lower_bound(_Link_type __x, _Link_type __y,
01635                    const _Key& __k)
01636     {
01637       while (__x != 0)
01638         if (!_M_impl._M_key_compare(_S_key(__x), __k))
01639           __y = __x, __x = _S_left(__x);
01640         else
01641           __x = _S_right(__x);
01642       return iterator(__y);
01643     }
01644 
01645   template<typename _Key, typename _Val, typename _KeyOfValue,
01646            typename _Compare, typename _Alloc>
01647     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01648                       _Compare, _Alloc>::const_iterator
01649     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01650     _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
01651                    const _Key& __k) const
01652     {
01653       while (__x != 0)
01654         if (!_M_impl._M_key_compare(_S_key(__x), __k))
01655           __y = __x, __x = _S_left(__x);
01656         else
01657           __x = _S_right(__x);
01658       return const_iterator(__y);
01659     }
01660 
01661   template<typename _Key, typename _Val, typename _KeyOfValue,
01662            typename _Compare, typename _Alloc>
01663     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01664                       _Compare, _Alloc>::iterator
01665     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01666     _M_upper_bound(_Link_type __x, _Link_type __y,
01667                    const _Key& __k)
01668     {
01669       while (__x != 0)
01670         if (_M_impl._M_key_compare(__k, _S_key(__x)))
01671           __y = __x, __x = _S_left(__x);
01672         else
01673           __x = _S_right(__x);
01674       return iterator(__y);
01675     }
01676 
01677   template<typename _Key, typename _Val, typename _KeyOfValue,
01678            typename _Compare, typename _Alloc>
01679     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01680                       _Compare, _Alloc>::const_iterator
01681     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01682     _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
01683                    const _Key& __k) const
01684     {
01685       while (__x != 0)
01686         if (_M_impl._M_key_compare(__k, _S_key(__x)))
01687           __y = __x, __x = _S_left(__x);
01688         else
01689           __x = _S_right(__x);
01690       return const_iterator(__y);
01691     }
01692 
01693   template<typename _Key, typename _Val, typename _KeyOfValue,
01694            typename _Compare, typename _Alloc>
01695     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01696                            _Compare, _Alloc>::iterator,
01697          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01698                            _Compare, _Alloc>::iterator>
01699     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01700     equal_range(const _Key& __k)
01701     {
01702       _Link_type __x = _M_begin();
01703       _Link_type __y = _M_end();
01704       while (__x != 0)
01705         {
01706           if (_M_impl._M_key_compare(_S_key(__x), __k))
01707             __x = _S_right(__x);
01708           else if (_M_impl._M_key_compare(__k, _S_key(__x)))
01709             __y = __x, __x = _S_left(__x);
01710           else
01711             {
01712               _Link_type __xu(__x), __yu(__y);
01713               __y = __x, __x = _S_left(__x);
01714               __xu = _S_right(__xu);
01715               return pair<iterator,
01716                           iterator>(_M_lower_bound(__x, __y, __k),
01717                                     _M_upper_bound(__xu, __yu, __k));
01718             }
01719         }
01720       return pair<iterator, iterator>(iterator(__y),
01721                                       iterator(__y));
01722     }
01723 
01724   template<typename _Key, typename _Val, typename _KeyOfValue,
01725            typename _Compare, typename _Alloc>
01726     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01727                            _Compare, _Alloc>::const_iterator,
01728          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01729                            _Compare, _Alloc>::const_iterator>
01730     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01731     equal_range(const _Key& __k) const
01732     {
01733       _Const_Link_type __x = _M_begin();
01734       _Const_Link_type __y = _M_end();
01735       while (__x != 0)
01736         {
01737           if (_M_impl._M_key_compare(_S_key(__x), __k))
01738             __x = _S_right(__x);
01739           else if (_M_impl._M_key_compare(__k, _S_key(__x)))
01740             __y = __x, __x = _S_left(__x);
01741           else
01742             {
01743               _Const_Link_type __xu(__x), __yu(__y);
01744               __y = __x, __x = _S_left(__x);
01745               __xu = _S_right(__xu);
01746               return pair<const_iterator,
01747                           const_iterator>(_M_lower_bound(__x, __y, __k),
01748                                           _M_upper_bound(__xu, __yu, __k));
01749             }
01750         }
01751       return pair<const_iterator, const_iterator>(const_iterator(__y),
01752                                                   const_iterator(__y));
01753     }
01754 
01755   template<typename _Key, typename _Val, typename _KeyOfValue,
01756            typename _Compare, typename _Alloc>
01757     void
01758     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01759     swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
01760 #if __cplusplus >= 201103L
01761     noexcept(_Alloc_traits::_S_nothrow_swap())
01762 #endif
01763     {
01764       if (_M_root() == 0)
01765         {
01766           if (__t._M_root() != 0)
01767             {
01768               _M_root() = __t._M_root();
01769               _M_leftmost() = __t._M_leftmost();
01770               _M_rightmost() = __t._M_rightmost();
01771               _M_root()->_M_parent = _M_end();
01772               _M_impl._M_node_count = __t._M_impl._M_node_count;
01773               
01774               __t._M_impl._M_reset();
01775             }
01776         }
01777       else if (__t._M_root() == 0)
01778         {
01779           __t._M_root() = _M_root();
01780           __t._M_leftmost() = _M_leftmost();
01781           __t._M_rightmost() = _M_rightmost();
01782           __t._M_root()->_M_parent = __t._M_end();
01783           __t._M_impl._M_node_count = _M_impl._M_node_count;
01784           
01785           _M_impl._M_reset();
01786         }
01787       else
01788         {
01789           std::swap(_M_root(),__t._M_root());
01790           std::swap(_M_leftmost(),__t._M_leftmost());
01791           std::swap(_M_rightmost(),__t._M_rightmost());
01792           
01793           _M_root()->_M_parent = _M_end();
01794           __t._M_root()->_M_parent = __t._M_end();
01795           std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
01796         }
01797       // No need to swap header's color as it does not change.
01798       std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
01799 
01800       _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
01801                                 __t._M_get_Node_allocator());
01802     }
01803 
01804   template<typename _Key, typename _Val, typename _KeyOfValue,
01805            typename _Compare, typename _Alloc>
01806     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01807                            _Compare, _Alloc>::_Base_ptr,
01808          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01809                            _Compare, _Alloc>::_Base_ptr>
01810     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01811     _M_get_insert_unique_pos(const key_type& __k)
01812     {
01813       typedef pair<_Base_ptr, _Base_ptr> _Res;
01814       _Link_type __x = _M_begin();
01815       _Link_type __y = _M_end();
01816       bool __comp = true;
01817       while (__x != 0)
01818         {
01819           __y = __x;
01820           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
01821           __x = __comp ? _S_left(__x) : _S_right(__x);
01822         }
01823       iterator __j = iterator(__y);
01824       if (__comp)
01825         {
01826           if (__j == begin())
01827             return _Res(__x, __y);
01828           else
01829             --__j;
01830         }
01831       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
01832         return _Res(__x, __y);
01833       return _Res(__j._M_node, 0);
01834     }
01835 
01836   template<typename _Key, typename _Val, typename _KeyOfValue,
01837            typename _Compare, typename _Alloc>
01838     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01839                            _Compare, _Alloc>::_Base_ptr,
01840          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01841                            _Compare, _Alloc>::_Base_ptr>
01842     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01843     _M_get_insert_equal_pos(const key_type& __k)
01844     {
01845       typedef pair<_Base_ptr, _Base_ptr> _Res;
01846       _Link_type __x = _M_begin();
01847       _Link_type __y = _M_end();
01848       while (__x != 0)
01849         {
01850           __y = __x;
01851           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
01852                 _S_left(__x) : _S_right(__x);
01853         }
01854       return _Res(__x, __y);
01855     }
01856 
01857   template<typename _Key, typename _Val, typename _KeyOfValue,
01858            typename _Compare, typename _Alloc>
01859 #if __cplusplus >= 201103L
01860     template<typename _Arg>
01861 #endif
01862     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01863                            _Compare, _Alloc>::iterator, bool>
01864     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01865 #if __cplusplus >= 201103L
01866     _M_insert_unique(_Arg&& __v)
01867 #else
01868     _M_insert_unique(const _Val& __v)
01869 #endif
01870     {
01871       typedef pair<iterator, bool> _Res;
01872       pair<_Base_ptr, _Base_ptr> __res
01873         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
01874 
01875       if (__res.second)
01876         {
01877           _Alloc_node __an(*this);
01878           return _Res(_M_insert_(__res.first, __res.second,
01879                                  _GLIBCXX_FORWARD(_Arg, __v), __an),
01880                       true);
01881         }
01882 
01883       return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
01884     }
01885 
01886   template<typename _Key, typename _Val, typename _KeyOfValue,
01887            typename _Compare, typename _Alloc>
01888 #if __cplusplus >= 201103L
01889     template<typename _Arg>
01890 #endif
01891     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01892     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01893 #if __cplusplus >= 201103L
01894     _M_insert_equal(_Arg&& __v)
01895 #else
01896     _M_insert_equal(const _Val& __v)
01897 #endif
01898     {
01899       pair<_Base_ptr, _Base_ptr> __res
01900         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
01901       _Alloc_node __an(*this);
01902       return _M_insert_(__res.first, __res.second,
01903                         _GLIBCXX_FORWARD(_Arg, __v), __an);
01904     }
01905 
01906   template<typename _Key, typename _Val, typename _KeyOfValue,
01907            typename _Compare, typename _Alloc>
01908     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01909                            _Compare, _Alloc>::_Base_ptr,
01910          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01911                            _Compare, _Alloc>::_Base_ptr>
01912     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01913     _M_get_insert_hint_unique_pos(const_iterator __position,
01914                                   const key_type& __k)
01915     {
01916       iterator __pos = __position._M_const_cast();
01917       typedef pair<_Base_ptr, _Base_ptr> _Res;
01918 
01919       // end()
01920       if (__pos._M_node == _M_end())
01921         {
01922           if (size() > 0
01923               && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
01924             return _Res(0, _M_rightmost());
01925           else
01926             return _M_get_insert_unique_pos(__k);
01927         }
01928       else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
01929         {
01930           // First, try before...
01931           iterator __before = __pos;
01932           if (__pos._M_node == _M_leftmost()) // begin()
01933             return _Res(_M_leftmost(), _M_leftmost());
01934           else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
01935             {
01936               if (_S_right(__before._M_node) == 0)
01937                 return _Res(0, __before._M_node);
01938               else
01939                 return _Res(__pos._M_node, __pos._M_node);
01940             }
01941           else
01942             return _M_get_insert_unique_pos(__k);
01943         }
01944       else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
01945         {
01946           // ... then try after.
01947           iterator __after = __pos;
01948           if (__pos._M_node == _M_rightmost())
01949             return _Res(0, _M_rightmost());
01950           else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
01951             {
01952               if (_S_right(__pos._M_node) == 0)
01953                 return _Res(0, __pos._M_node);
01954               else
01955                 return _Res(__after._M_node, __after._M_node);
01956             }
01957           else
01958             return _M_get_insert_unique_pos(__k);
01959         }
01960       else
01961         // Equivalent keys.
01962         return _Res(__pos._M_node, 0);
01963     }
01964 
01965   template<typename _Key, typename _Val, typename _KeyOfValue,
01966            typename _Compare, typename _Alloc>
01967 #if __cplusplus >= 201103L
01968     template<typename _Arg, typename _NodeGen>
01969 #else
01970     template<typename _NodeGen>
01971 #endif
01972       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01973       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01974       _M_insert_unique_(const_iterator __position,
01975 #if __cplusplus >= 201103L
01976                         _Arg&& __v,
01977 #else
01978                         const _Val& __v,
01979 #endif
01980                         _NodeGen& __node_gen)
01981     {
01982       pair<_Base_ptr, _Base_ptr> __res
01983         = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
01984 
01985       if (__res.second)
01986         return _M_insert_(__res.first, __res.second,
01987                           _GLIBCXX_FORWARD(_Arg, __v),
01988                           __node_gen);
01989       return iterator(static_cast<_Link_type>(__res.first));
01990     }
01991 
01992   template<typename _Key, typename _Val, typename _KeyOfValue,
01993            typename _Compare, typename _Alloc>
01994     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01995                            _Compare, _Alloc>::_Base_ptr,
01996          typename _Rb_tree<_Key, _Val, _KeyOfValue,
01997                            _Compare, _Alloc>::_Base_ptr>
01998     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01999     _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
02000     {
02001       iterator __pos = __position._M_const_cast();
02002       typedef pair<_Base_ptr, _Base_ptr> _Res;
02003 
02004       // end()
02005       if (__pos._M_node == _M_end())
02006         {
02007           if (size() > 0
02008               && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
02009             return _Res(0, _M_rightmost());
02010           else
02011             return _M_get_insert_equal_pos(__k);
02012         }
02013       else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
02014         {
02015           // First, try before...
02016           iterator __before = __pos;
02017           if (__pos._M_node == _M_leftmost()) // begin()
02018             return _Res(_M_leftmost(), _M_leftmost());
02019           else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
02020             {
02021               if (_S_right(__before._M_node) == 0)
02022                 return _Res(0, __before._M_node);
02023               else
02024                 return _Res(__pos._M_node, __pos._M_node);
02025             }
02026           else
02027             return _M_get_insert_equal_pos(__k);
02028         }
02029       else
02030         {
02031           // ... then try after.  
02032           iterator __after = __pos;
02033           if (__pos._M_node == _M_rightmost())
02034             return _Res(0, _M_rightmost());
02035           else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
02036             {
02037               if (_S_right(__pos._M_node) == 0)
02038                 return _Res(0, __pos._M_node);
02039               else
02040                 return _Res(__after._M_node, __after._M_node);
02041             }
02042           else
02043             return _Res(0, 0);
02044         }
02045     }
02046 
02047   template<typename _Key, typename _Val, typename _KeyOfValue,
02048            typename _Compare, typename _Alloc>
02049 #if __cplusplus >= 201103L
02050     template<typename _Arg, typename _NodeGen>
02051 #else
02052     template<typename _NodeGen>
02053 #endif
02054       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02055       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02056       _M_insert_equal_(const_iterator __position,
02057 #if __cplusplus >= 201103L
02058                        _Arg&& __v,
02059 #else
02060                        const _Val& __v,
02061 #endif
02062                        _NodeGen& __node_gen)
02063       {
02064         pair<_Base_ptr, _Base_ptr> __res
02065           = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
02066 
02067         if (__res.second)
02068           return _M_insert_(__res.first, __res.second,
02069                             _GLIBCXX_FORWARD(_Arg, __v),
02070                             __node_gen);
02071 
02072         return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
02073       }
02074 
02075 #if __cplusplus >= 201103L
02076   template<typename _Key, typename _Val, typename _KeyOfValue,
02077            typename _Compare, typename _Alloc>
02078     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02079     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02080     _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
02081     {
02082       bool __insert_left = (__x != 0 || __p == _M_end()
02083                             || _M_impl._M_key_compare(_S_key(__z),
02084                                                       _S_key(__p)));
02085 
02086       _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
02087                                     this->_M_impl._M_header);
02088       ++_M_impl._M_node_count;
02089       return iterator(__z);
02090     }
02091 
02092   template<typename _Key, typename _Val, typename _KeyOfValue,
02093            typename _Compare, typename _Alloc>
02094     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02095     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02096     _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
02097     {
02098       bool __insert_left = (__p == _M_end()
02099                             || !_M_impl._M_key_compare(_S_key(__p),
02100                                                        _S_key(__z)));
02101 
02102       _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
02103                                     this->_M_impl._M_header);
02104       ++_M_impl._M_node_count;
02105       return iterator(__z);
02106     }
02107 
02108   template<typename _Key, typename _Val, typename _KeyOfValue,
02109            typename _Compare, typename _Alloc>
02110     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02111     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02112     _M_insert_equal_lower_node(_Link_type __z)
02113     {
02114       _Link_type __x = _M_begin();
02115       _Link_type __y = _M_end();
02116       while (__x != 0)
02117         {
02118           __y = __x;
02119           __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
02120                 _S_left(__x) : _S_right(__x);
02121         }
02122       return _M_insert_lower_node(__y, __z);
02123     }
02124 
02125   template<typename _Key, typename _Val, typename _KeyOfValue,
02126            typename _Compare, typename _Alloc>
02127     template<typename... _Args>
02128       pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
02129                              _Compare, _Alloc>::iterator, bool>
02130       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02131       _M_emplace_unique(_Args&&... __args)
02132       {
02133         _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
02134 
02135         __try
02136           {
02137             typedef pair<iterator, bool> _Res;
02138             auto __res = _M_get_insert_unique_pos(_S_key(__z));
02139             if (__res.second)
02140               return _Res(_M_insert_node(__res.first, __res.second, __z), true);
02141         
02142             _M_drop_node(__z);
02143             return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
02144           }
02145         __catch(...)
02146           {
02147             _M_drop_node(__z);
02148             __throw_exception_again;
02149           }
02150       }
02151 
02152   template<typename _Key, typename _Val, typename _KeyOfValue,
02153            typename _Compare, typename _Alloc>
02154     template<typename... _Args>
02155       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02156       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02157       _M_emplace_equal(_Args&&... __args)
02158       {
02159         _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
02160 
02161         __try
02162           {
02163             auto __res = _M_get_insert_equal_pos(_S_key(__z));
02164             return _M_insert_node(__res.first, __res.second, __z);
02165           }
02166         __catch(...)
02167           {
02168             _M_drop_node(__z);
02169             __throw_exception_again;
02170           }
02171       }
02172 
02173   template<typename _Key, typename _Val, typename _KeyOfValue,
02174            typename _Compare, typename _Alloc>
02175     template<typename... _Args>
02176       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02177       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02178       _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
02179       {
02180         _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
02181 
02182         __try
02183           {
02184             auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
02185 
02186             if (__res.second)
02187               return _M_insert_node(__res.first, __res.second, __z);
02188 
02189             _M_drop_node(__z);
02190             return iterator(static_cast<_Link_type>(__res.first));
02191           }
02192         __catch(...)
02193           {
02194             _M_drop_node(__z);
02195             __throw_exception_again;
02196           }
02197       }
02198 
02199   template<typename _Key, typename _Val, typename _KeyOfValue,
02200            typename _Compare, typename _Alloc>
02201     template<typename... _Args>
02202       typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
02203       _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02204       _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
02205       {
02206         _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
02207 
02208         __try
02209           {
02210             auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
02211 
02212             if (__res.second)
02213               return _M_insert_node(__res.first, __res.second, __z);
02214 
02215             return _M_insert_equal_lower_node(__z);
02216           }
02217         __catch(...)
02218           {
02219             _M_drop_node(__z);
02220             __throw_exception_again;
02221           }
02222       }
02223 #endif
02224 
02225   template<typename _Key, typename _Val, typename _KoV,
02226            typename _Cmp, typename _Alloc>
02227     template<class _II>
02228       void
02229       _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
02230       _M_insert_unique(_II __first, _II __last)
02231       {
02232         _Alloc_node __an(*this);
02233         for (; __first != __last; ++__first)
02234           _M_insert_unique_(end(), *__first, __an);
02235       }
02236 
02237   template<typename _Key, typename _Val, typename _KoV,
02238            typename _Cmp, typename _Alloc>
02239     template<class _II>
02240       void
02241       _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
02242       _M_insert_equal(_II __first, _II __last)
02243       {
02244         _Alloc_node __an(*this);
02245         for (; __first != __last; ++__first)
02246           _M_insert_equal_(end(), *__first, __an);
02247       }
02248 
02249   template<typename _Key, typename _Val, typename _KeyOfValue,
02250            typename _Compare, typename _Alloc>
02251     void
02252     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02253     _M_erase_aux(const_iterator __position)
02254     {
02255       _Link_type __y =
02256         static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
02257                                 (const_cast<_Base_ptr>(__position._M_node),
02258                                  this->_M_impl._M_header));
02259       _M_drop_node(__y);
02260       --_M_impl._M_node_count;
02261     }
02262 
02263   template<typename _Key, typename _Val, typename _KeyOfValue,
02264            typename _Compare, typename _Alloc>
02265     void
02266     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02267     _M_erase_aux(const_iterator __first, const_iterator __last)
02268     {
02269       if (__first == begin() && __last == end())
02270         clear();
02271       else
02272         while (__first != __last)
02273           erase(__first++);
02274     }
02275 
02276   template<typename _Key, typename _Val, typename _KeyOfValue,
02277            typename _Compare, typename _Alloc>
02278     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
02279     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02280     erase(const _Key& __x)
02281     {
02282       pair<iterator, iterator> __p = equal_range(__x);
02283       const size_type __old_size = size();
02284       erase(__p.first, __p.second);
02285       return __old_size - size();
02286     }
02287 
02288   template<typename _Key, typename _Val, typename _KeyOfValue,
02289            typename _Compare, typename _Alloc>
02290     void
02291     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02292     erase(const _Key* __first, const _Key* __last)
02293     {
02294       while (__first != __last)
02295         erase(*__first++);
02296     }
02297 
02298   template<typename _Key, typename _Val, typename _KeyOfValue,
02299            typename _Compare, typename _Alloc>
02300     typename _Rb_tree<_Key, _Val, _KeyOfValue,
02301                       _Compare, _Alloc>::iterator
02302     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02303     find(const _Key& __k)
02304     {
02305       iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
02306       return (__j == end()
02307               || _M_impl._M_key_compare(__k,
02308                                         _S_key(__j._M_node))) ? end() : __j;
02309     }
02310 
02311   template<typename _Key, typename _Val, typename _KeyOfValue,
02312            typename _Compare, typename _Alloc>
02313     typename _Rb_tree<_Key, _Val, _KeyOfValue,
02314                       _Compare, _Alloc>::const_iterator
02315     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02316     find(const _Key& __k) const
02317     {
02318       const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
02319       return (__j == end()
02320               || _M_impl._M_key_compare(__k, 
02321                                         _S_key(__j._M_node))) ? end() : __j;
02322     }
02323 
02324   template<typename _Key, typename _Val, typename _KeyOfValue,
02325            typename _Compare, typename _Alloc>
02326     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
02327     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
02328     count(const _Key& __k) const
02329     {
02330       pair<const_iterator, const_iterator> __p = equal_range(__k);
02331       const size_type __n = std::distance(__p.first, __p.second);
02332       return __n;
02333     }
02334 
02335   _GLIBCXX_PURE unsigned int
02336   _Rb_tree_black_count(const _Rb_tree_node_base* __node,
02337                        const _Rb_tree_node_base* __root) throw ();
02338 
02339   template<typename _Key, typename _Val, typename _KeyOfValue,
02340            typename _Compare, typename _Alloc>
02341     bool
02342     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
02343     {
02344       if (_M_impl._M_node_count == 0 || begin() == end())
02345         return _M_impl._M_node_count == 0 && begin() == end()
02346                && this->_M_impl._M_header._M_left == _M_end()
02347                && this->_M_impl._M_header._M_right == _M_end();
02348 
02349       unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
02350       for (const_iterator __it = begin(); __it != end(); ++__it)
02351         {
02352           _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
02353           _Const_Link_type __L = _S_left(__x);
02354           _Const_Link_type __R = _S_right(__x);
02355 
02356           if (__x->_M_color == _S_red)
02357             if ((__L && __L->_M_color == _S_red)
02358                 || (__R && __R->_M_color == _S_red))
02359               return false;
02360 
02361           if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
02362             return false;
02363           if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
02364             return false;
02365 
02366           if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
02367             return false;
02368         }
02369 
02370       if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
02371         return false;
02372       if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
02373         return false;
02374       return true;
02375     }
02376 
02377 _GLIBCXX_END_NAMESPACE_VERSION
02378 } // namespace
02379 
02380 #endif