libstdc++
stl_tree.h
Go to the documentation of this file.
00001 // RB tree implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
00004 // 2009, 2010, 2011
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1996,1997
00030  * Silicon Graphics Computer Systems, Inc.
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Silicon Graphics makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1994
00042  * Hewlett-Packard Company
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Hewlett-Packard Company makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  *
00052  *
00053  */
00054 
00055 /** @file bits/stl_tree.h
00056  *  This is an internal header file, included by other library headers.
00057  *  Do not attempt to use it directly. @headername{map or set}
00058  */
00059 
00060 #ifndef _STL_TREE_H
00061 #define _STL_TREE_H 1
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 
00068 namespace std _GLIBCXX_VISIBILITY(default)
00069 {
00070 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00071 
00072   // Red-black tree class, designed for use in implementing STL
00073   // associative containers (set, multiset, map, and multimap). The
00074   // insertion and deletion algorithms are based on those in Cormen,
00075   // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
00076   // 1990), except that
00077   //
00078   // (1) the header cell is maintained with links not only to the root
00079   // but also to the leftmost node of the tree, to enable constant
00080   // time begin(), and to the rightmost node of the tree, to enable
00081   // linear time performance when used with the generic set algorithms
00082   // (set_union, etc.)
00083   // 
00084   // (2) when a node being deleted has two children its successor node
00085   // is relinked into its place, rather than copied, so that the only
00086   // iterators invalidated are those referring to the deleted node.
00087 
00088   enum _Rb_tree_color { _S_red = false, _S_black = true };
00089 
00090   struct _Rb_tree_node_base
00091   {
00092     typedef _Rb_tree_node_base* _Base_ptr;
00093     typedef const _Rb_tree_node_base* _Const_Base_ptr;
00094 
00095     _Rb_tree_color  _M_color;
00096     _Base_ptr       _M_parent;
00097     _Base_ptr       _M_left;
00098     _Base_ptr       _M_right;
00099 
00100     static _Base_ptr
00101     _S_minimum(_Base_ptr __x)
00102     {
00103       while (__x->_M_left != 0) __x = __x->_M_left;
00104       return __x;
00105     }
00106 
00107     static _Const_Base_ptr
00108     _S_minimum(_Const_Base_ptr __x)
00109     {
00110       while (__x->_M_left != 0) __x = __x->_M_left;
00111       return __x;
00112     }
00113 
00114     static _Base_ptr
00115     _S_maximum(_Base_ptr __x)
00116     {
00117       while (__x->_M_right != 0) __x = __x->_M_right;
00118       return __x;
00119     }
00120 
00121     static _Const_Base_ptr
00122     _S_maximum(_Const_Base_ptr __x)
00123     {
00124       while (__x->_M_right != 0) __x = __x->_M_right;
00125       return __x;
00126     }
00127   };
00128 
00129   template<typename _Val>
00130     struct _Rb_tree_node : public _Rb_tree_node_base
00131     {
00132       typedef _Rb_tree_node<_Val>* _Link_type;
00133       _Val _M_value_field;
00134 
00135 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00136       template<typename... _Args>
00137         _Rb_tree_node(_Args&&... __args)
00138     : _Rb_tree_node_base(),
00139       _M_value_field(std::forward<_Args>(__args)...) { }
00140 #endif
00141     };
00142 
00143   _GLIBCXX_PURE _Rb_tree_node_base*
00144   _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
00145 
00146   _GLIBCXX_PURE const _Rb_tree_node_base*
00147   _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
00148 
00149   _GLIBCXX_PURE _Rb_tree_node_base*
00150   _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
00151 
00152   _GLIBCXX_PURE const _Rb_tree_node_base*
00153   _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
00154 
00155   template<typename _Tp>
00156     struct _Rb_tree_iterator
00157     {
00158       typedef _Tp  value_type;
00159       typedef _Tp& reference;
00160       typedef _Tp* pointer;
00161 
00162       typedef bidirectional_iterator_tag iterator_category;
00163       typedef ptrdiff_t                  difference_type;
00164 
00165       typedef _Rb_tree_iterator<_Tp>        _Self;
00166       typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
00167       typedef _Rb_tree_node<_Tp>*           _Link_type;
00168 
00169       _Rb_tree_iterator()
00170       : _M_node() { }
00171 
00172       explicit
00173       _Rb_tree_iterator(_Link_type __x)
00174       : _M_node(__x) { }
00175 
00176       reference
00177       operator*() const
00178       { return static_cast<_Link_type>(_M_node)->_M_value_field; }
00179 
00180       pointer
00181       operator->() const
00182       { return std::__addressof(static_cast<_Link_type>
00183                 (_M_node)->_M_value_field); }
00184 
00185       _Self&
00186       operator++()
00187       {
00188     _M_node = _Rb_tree_increment(_M_node);
00189     return *this;
00190       }
00191 
00192       _Self
00193       operator++(int)
00194       {
00195     _Self __tmp = *this;
00196     _M_node = _Rb_tree_increment(_M_node);
00197     return __tmp;
00198       }
00199 
00200       _Self&
00201       operator--()
00202       {
00203     _M_node = _Rb_tree_decrement(_M_node);
00204     return *this;
00205       }
00206 
00207       _Self
00208       operator--(int)
00209       {
00210     _Self __tmp = *this;
00211     _M_node = _Rb_tree_decrement(_M_node);
00212     return __tmp;
00213       }
00214 
00215       bool
00216       operator==(const _Self& __x) const
00217       { return _M_node == __x._M_node; }
00218 
00219       bool
00220       operator!=(const _Self& __x) const
00221       { return _M_node != __x._M_node; }
00222 
00223       _Base_ptr _M_node;
00224   };
00225 
00226   template<typename _Tp>
00227     struct _Rb_tree_const_iterator
00228     {
00229       typedef _Tp        value_type;
00230       typedef const _Tp& reference;
00231       typedef const _Tp* pointer;
00232 
00233       typedef _Rb_tree_iterator<_Tp> iterator;
00234 
00235       typedef bidirectional_iterator_tag iterator_category;
00236       typedef ptrdiff_t                  difference_type;
00237 
00238       typedef _Rb_tree_const_iterator<_Tp>        _Self;
00239       typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
00240       typedef const _Rb_tree_node<_Tp>*           _Link_type;
00241 
00242       _Rb_tree_const_iterator()
00243       : _M_node() { }
00244 
00245       explicit
00246       _Rb_tree_const_iterator(_Link_type __x)
00247       : _M_node(__x) { }
00248 
00249       _Rb_tree_const_iterator(const iterator& __it)
00250       : _M_node(__it._M_node) { }
00251 
00252       iterator
00253       _M_const_cast() const
00254       { return iterator(static_cast<typename iterator::_Link_type>
00255             (const_cast<typename iterator::_Base_ptr>(_M_node))); }
00256 
00257       reference
00258       operator*() const
00259       { return static_cast<_Link_type>(_M_node)->_M_value_field; }
00260 
00261       pointer
00262       operator->() const
00263       { return std::__addressof(static_cast<_Link_type>
00264                 (_M_node)->_M_value_field); }
00265 
00266       _Self&
00267       operator++()
00268       {
00269     _M_node = _Rb_tree_increment(_M_node);
00270     return *this;
00271       }
00272 
00273       _Self
00274       operator++(int)
00275       {
00276     _Self __tmp = *this;
00277     _M_node = _Rb_tree_increment(_M_node);
00278     return __tmp;
00279       }
00280 
00281       _Self&
00282       operator--()
00283       {
00284     _M_node = _Rb_tree_decrement(_M_node);
00285     return *this;
00286       }
00287 
00288       _Self
00289       operator--(int)
00290       {
00291     _Self __tmp = *this;
00292     _M_node = _Rb_tree_decrement(_M_node);
00293     return __tmp;
00294       }
00295 
00296       bool
00297       operator==(const _Self& __x) const
00298       { return _M_node == __x._M_node; }
00299 
00300       bool
00301       operator!=(const _Self& __x) const
00302       { return _M_node != __x._M_node; }
00303 
00304       _Base_ptr _M_node;
00305     };
00306 
00307   template<typename _Val>
00308     inline bool
00309     operator==(const _Rb_tree_iterator<_Val>& __x,
00310                const _Rb_tree_const_iterator<_Val>& __y)
00311     { return __x._M_node == __y._M_node; }
00312 
00313   template<typename _Val>
00314     inline bool
00315     operator!=(const _Rb_tree_iterator<_Val>& __x,
00316                const _Rb_tree_const_iterator<_Val>& __y)
00317     { return __x._M_node != __y._M_node; }
00318 
00319   void
00320   _Rb_tree_insert_and_rebalance(const bool __insert_left,
00321                                 _Rb_tree_node_base* __x,
00322                                 _Rb_tree_node_base* __p,
00323                                 _Rb_tree_node_base& __header) throw ();
00324 
00325   _Rb_tree_node_base*
00326   _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
00327                    _Rb_tree_node_base& __header) throw ();
00328 
00329 
00330   template<typename _Key, typename _Val, typename _KeyOfValue,
00331            typename _Compare, typename _Alloc = allocator<_Val> >
00332     class _Rb_tree
00333     {
00334       typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
00335               _Node_allocator;
00336 
00337     protected:
00338       typedef _Rb_tree_node_base* _Base_ptr;
00339       typedef const _Rb_tree_node_base* _Const_Base_ptr;
00340 
00341     public:
00342       typedef _Key key_type;
00343       typedef _Val value_type;
00344       typedef value_type* pointer;
00345       typedef const value_type* const_pointer;
00346       typedef value_type& reference;
00347       typedef const value_type& const_reference;
00348       typedef _Rb_tree_node<_Val>* _Link_type;
00349       typedef const _Rb_tree_node<_Val>* _Const_Link_type;
00350       typedef size_t size_type;
00351       typedef ptrdiff_t difference_type;
00352       typedef _Alloc allocator_type;
00353 
00354       _Node_allocator&
00355       _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
00356       { return *static_cast<_Node_allocator*>(&this->_M_impl); }
00357       
00358       const _Node_allocator&
00359       _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
00360       { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
00361 
00362       allocator_type
00363       get_allocator() const _GLIBCXX_NOEXCEPT
00364       { return allocator_type(_M_get_Node_allocator()); }
00365 
00366     protected:
00367       _Link_type
00368       _M_get_node()
00369       { return _M_impl._Node_allocator::allocate(1); }
00370 
00371       void
00372       _M_put_node(_Link_type __p)
00373       { _M_impl._Node_allocator::deallocate(__p, 1); }
00374 
00375 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00376       _Link_type
00377       _M_create_node(const value_type& __x)
00378       {
00379     _Link_type __tmp = _M_get_node();
00380     __try
00381       { get_allocator().construct
00382           (std::__addressof(__tmp->_M_value_field), __x); }
00383     __catch(...)
00384       {
00385         _M_put_node(__tmp);
00386         __throw_exception_again;
00387       }
00388     return __tmp;
00389       }
00390 
00391       void
00392       _M_destroy_node(_Link_type __p)
00393       {
00394     get_allocator().destroy(std::__addressof(__p->_M_value_field));
00395     _M_put_node(__p);
00396       }
00397 #else
00398       template<typename... _Args>
00399         _Link_type
00400         _M_create_node(_Args&&... __args)
00401     {
00402       _Link_type __tmp = _M_get_node();
00403       __try
00404         {
00405           _M_get_Node_allocator().construct(__tmp,
00406                          std::forward<_Args>(__args)...);
00407         }
00408       __catch(...)
00409         {
00410           _M_put_node(__tmp);
00411           __throw_exception_again;
00412         }
00413       return __tmp;
00414     }
00415 
00416       void
00417       _M_destroy_node(_Link_type __p)
00418       {
00419     _M_get_Node_allocator().destroy(__p);
00420     _M_put_node(__p);
00421       }
00422 #endif
00423 
00424       _Link_type
00425       _M_clone_node(_Const_Link_type __x)
00426       {
00427     _Link_type __tmp = _M_create_node(__x->_M_value_field);
00428     __tmp->_M_color = __x->_M_color;
00429     __tmp->_M_left = 0;
00430     __tmp->_M_right = 0;
00431     return __tmp;
00432       }
00433 
00434     protected:
00435       template<typename _Key_compare, 
00436            bool _Is_pod_comparator = __is_pod(_Key_compare)>
00437         struct _Rb_tree_impl : public _Node_allocator
00438         {
00439       _Key_compare      _M_key_compare;
00440       _Rb_tree_node_base    _M_header;
00441       size_type         _M_node_count; // Keeps track of size of tree.
00442 
00443       _Rb_tree_impl()
00444       : _Node_allocator(), _M_key_compare(), _M_header(),
00445         _M_node_count(0)
00446       { _M_initialize(); }
00447 
00448       _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
00449       : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
00450         _M_node_count(0)
00451       { _M_initialize(); }
00452 
00453 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00454       _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
00455       : _Node_allocator(std::move(__a)), _M_key_compare(__comp),
00456         _M_header(), _M_node_count(0)
00457       { _M_initialize(); }
00458 #endif
00459 
00460     private:
00461       void
00462       _M_initialize()
00463       {
00464         this->_M_header._M_color = _S_red;
00465         this->_M_header._M_parent = 0;
00466         this->_M_header._M_left = &this->_M_header;
00467         this->_M_header._M_right = &this->_M_header;
00468       }     
00469     };
00470 
00471       _Rb_tree_impl<_Compare> _M_impl;
00472 
00473     protected:
00474       _Base_ptr&
00475       _M_root()
00476       { return this->_M_impl._M_header._M_parent; }
00477 
00478       _Const_Base_ptr
00479       _M_root() const
00480       { return this->_M_impl._M_header._M_parent; }
00481 
00482       _Base_ptr&
00483       _M_leftmost()
00484       { return this->_M_impl._M_header._M_left; }
00485 
00486       _Const_Base_ptr
00487       _M_leftmost() const
00488       { return this->_M_impl._M_header._M_left; }
00489 
00490       _Base_ptr&
00491       _M_rightmost()
00492       { return this->_M_impl._M_header._M_right; }
00493 
00494       _Const_Base_ptr
00495       _M_rightmost() const
00496       { return this->_M_impl._M_header._M_right; }
00497 
00498       _Link_type
00499       _M_begin()
00500       { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
00501 
00502       _Const_Link_type
00503       _M_begin() const
00504       {
00505     return static_cast<_Const_Link_type>
00506       (this->_M_impl._M_header._M_parent);
00507       }
00508 
00509       _Link_type
00510       _M_end()
00511       { return static_cast<_Link_type>(&this->_M_impl._M_header); }
00512 
00513       _Const_Link_type
00514       _M_end() const
00515       { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
00516 
00517       static const_reference
00518       _S_value(_Const_Link_type __x)
00519       { return __x->_M_value_field; }
00520 
00521       static const _Key&
00522       _S_key(_Const_Link_type __x)
00523       { return _KeyOfValue()(_S_value(__x)); }
00524 
00525       static _Link_type
00526       _S_left(_Base_ptr __x)
00527       { return static_cast<_Link_type>(__x->_M_left); }
00528 
00529       static _Const_Link_type
00530       _S_left(_Const_Base_ptr __x)
00531       { return static_cast<_Const_Link_type>(__x->_M_left); }
00532 
00533       static _Link_type
00534       _S_right(_Base_ptr __x)
00535       { return static_cast<_Link_type>(__x->_M_right); }
00536 
00537       static _Const_Link_type
00538       _S_right(_Const_Base_ptr __x)
00539       { return static_cast<_Const_Link_type>(__x->_M_right); }
00540 
00541       static const_reference
00542       _S_value(_Const_Base_ptr __x)
00543       { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
00544 
00545       static const _Key&
00546       _S_key(_Const_Base_ptr __x)
00547       { return _KeyOfValue()(_S_value(__x)); }
00548 
00549       static _Base_ptr
00550       _S_minimum(_Base_ptr __x)
00551       { return _Rb_tree_node_base::_S_minimum(__x); }
00552 
00553       static _Const_Base_ptr
00554       _S_minimum(_Const_Base_ptr __x)
00555       { return _Rb_tree_node_base::_S_minimum(__x); }
00556 
00557       static _Base_ptr
00558       _S_maximum(_Base_ptr __x)
00559       { return _Rb_tree_node_base::_S_maximum(__x); }
00560 
00561       static _Const_Base_ptr
00562       _S_maximum(_Const_Base_ptr __x)
00563       { return _Rb_tree_node_base::_S_maximum(__x); }
00564 
00565     public:
00566       typedef _Rb_tree_iterator<value_type>       iterator;
00567       typedef _Rb_tree_const_iterator<value_type> const_iterator;
00568 
00569       typedef std::reverse_iterator<iterator>       reverse_iterator;
00570       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00571 
00572     private:
00573 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00574       template<typename _Arg>
00575         iterator
00576         _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __y, _Arg&& __v);
00577 
00578       template<typename _Arg>
00579         iterator
00580         _M_insert_lower(_Base_ptr __x, _Base_ptr __y, _Arg&& __v);
00581 
00582       template<typename _Arg>
00583         iterator
00584         _M_insert_equal_lower(_Arg&& __x);
00585 #else
00586       iterator
00587       _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __y,
00588          const value_type& __v);
00589 
00590       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00591       // 233. Insertion hints in associative containers.
00592       iterator
00593       _M_insert_lower(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
00594 
00595       iterator
00596       _M_insert_equal_lower(const value_type& __x);
00597 #endif
00598 
00599       _Link_type
00600       _M_copy(_Const_Link_type __x, _Link_type __p);
00601 
00602       void
00603       _M_erase(_Link_type __x);
00604 
00605       iterator
00606       _M_lower_bound(_Link_type __x, _Link_type __y,
00607              const _Key& __k);
00608 
00609       const_iterator
00610       _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
00611              const _Key& __k) const;
00612 
00613       iterator
00614       _M_upper_bound(_Link_type __x, _Link_type __y,
00615              const _Key& __k);
00616 
00617       const_iterator
00618       _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
00619              const _Key& __k) const;
00620 
00621     public:
00622       // allocation/deallocation
00623       _Rb_tree() { }
00624 
00625       _Rb_tree(const _Compare& __comp,
00626            const allocator_type& __a = allocator_type())
00627       : _M_impl(__comp, _Node_allocator(__a)) { }
00628 
00629       _Rb_tree(const _Rb_tree& __x)
00630       : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
00631       {
00632     if (__x._M_root() != 0)
00633       {
00634         _M_root() = _M_copy(__x._M_begin(), _M_end());
00635         _M_leftmost() = _S_minimum(_M_root());
00636         _M_rightmost() = _S_maximum(_M_root());
00637         _M_impl._M_node_count = __x._M_impl._M_node_count;
00638       }
00639       }
00640 
00641 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00642       _Rb_tree(_Rb_tree&& __x);
00643 #endif
00644 
00645       ~_Rb_tree() _GLIBCXX_NOEXCEPT
00646       { _M_erase(_M_begin()); }
00647 
00648       _Rb_tree&
00649       operator=(const _Rb_tree& __x);
00650 
00651       // Accessors.
00652       _Compare
00653       key_comp() const
00654       { return _M_impl._M_key_compare; }
00655 
00656       iterator
00657       begin() _GLIBCXX_NOEXCEPT
00658       { 
00659     return iterator(static_cast<_Link_type>
00660             (this->_M_impl._M_header._M_left));
00661       }
00662 
00663       const_iterator
00664       begin() const _GLIBCXX_NOEXCEPT
00665       { 
00666     return const_iterator(static_cast<_Const_Link_type>
00667                   (this->_M_impl._M_header._M_left));
00668       }
00669 
00670       iterator
00671       end() _GLIBCXX_NOEXCEPT
00672       { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
00673 
00674       const_iterator
00675       end() const _GLIBCXX_NOEXCEPT
00676       { 
00677     return const_iterator(static_cast<_Const_Link_type>
00678                   (&this->_M_impl._M_header));
00679       }
00680 
00681       reverse_iterator
00682       rbegin() _GLIBCXX_NOEXCEPT
00683       { return reverse_iterator(end()); }
00684 
00685       const_reverse_iterator
00686       rbegin() const _GLIBCXX_NOEXCEPT
00687       { return const_reverse_iterator(end()); }
00688 
00689       reverse_iterator
00690       rend() _GLIBCXX_NOEXCEPT
00691       { return reverse_iterator(begin()); }
00692 
00693       const_reverse_iterator
00694       rend() const _GLIBCXX_NOEXCEPT
00695       { return const_reverse_iterator(begin()); }
00696 
00697       bool
00698       empty() const _GLIBCXX_NOEXCEPT
00699       { return _M_impl._M_node_count == 0; }
00700 
00701       size_type
00702       size() const _GLIBCXX_NOEXCEPT 
00703       { return _M_impl._M_node_count; }
00704 
00705       size_type
00706       max_size() const _GLIBCXX_NOEXCEPT
00707       { return _M_get_Node_allocator().max_size(); }
00708 
00709       void
00710       swap(_Rb_tree& __t);      
00711 
00712       // Insert/erase.
00713 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00714       template<typename _Arg>
00715         pair<iterator, bool>
00716         _M_insert_unique(_Arg&& __x);
00717 
00718       template<typename _Arg>
00719         iterator
00720         _M_insert_equal(_Arg&& __x);
00721 
00722       template<typename _Arg>
00723         iterator
00724         _M_insert_unique_(const_iterator __position, _Arg&& __x);
00725 
00726       template<typename _Arg>
00727         iterator
00728         _M_insert_equal_(const_iterator __position, _Arg&& __x);
00729 #else
00730       pair<iterator, bool>
00731       _M_insert_unique(const value_type& __x);
00732 
00733       iterator
00734       _M_insert_equal(const value_type& __x);
00735 
00736       iterator
00737       _M_insert_unique_(const_iterator __position, const value_type& __x);
00738 
00739       iterator
00740       _M_insert_equal_(const_iterator __position, const value_type& __x);
00741 #endif
00742 
00743       template<typename _InputIterator>
00744         void
00745         _M_insert_unique(_InputIterator __first, _InputIterator __last);
00746 
00747       template<typename _InputIterator>
00748         void
00749         _M_insert_equal(_InputIterator __first, _InputIterator __last);
00750 
00751     private:
00752       void
00753       _M_erase_aux(const_iterator __position);
00754 
00755       void
00756       _M_erase_aux(const_iterator __first, const_iterator __last);
00757 
00758     public:
00759 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00760       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00761       // DR 130. Associative erase should return an iterator.
00762       iterator
00763       erase(const_iterator __position)
00764       {
00765     const_iterator __result = __position;
00766     ++__result;
00767     _M_erase_aux(__position);
00768     return __result._M_const_cast();
00769       }
00770 
00771       // LWG 2059.
00772       iterator
00773       erase(iterator __position)
00774       {
00775     iterator __result = __position;
00776     ++__result;
00777     _M_erase_aux(__position);
00778     return __result;
00779       }
00780 #else
00781       void
00782       erase(iterator __position)
00783       { _M_erase_aux(__position); }
00784 
00785       void
00786       erase(const_iterator __position)
00787       { _M_erase_aux(__position); }
00788 #endif
00789       size_type
00790       erase(const key_type& __x);
00791 
00792 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00793       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00794       // DR 130. Associative erase should return an iterator.
00795       iterator
00796       erase(const_iterator __first, const_iterator __last)
00797       {
00798     _M_erase_aux(__first, __last);
00799     return __last._M_const_cast();
00800       }
00801 #else
00802       void
00803       erase(iterator __first, iterator __last)
00804       { _M_erase_aux(__first, __last); }
00805 
00806       void
00807       erase(const_iterator __first, const_iterator __last)
00808       { _M_erase_aux(__first, __last); }
00809 #endif
00810       void
00811       erase(const key_type* __first, const key_type* __last);
00812 
00813       void
00814       clear() _GLIBCXX_NOEXCEPT
00815       {
00816         _M_erase(_M_begin());
00817         _M_leftmost() = _M_end();
00818         _M_root() = 0;
00819         _M_rightmost() = _M_end();
00820         _M_impl._M_node_count = 0;
00821       }
00822 
00823       // Set operations.
00824       iterator
00825       find(const key_type& __k);
00826 
00827       const_iterator
00828       find(const key_type& __k) const;
00829 
00830       size_type
00831       count(const key_type& __k) const;
00832 
00833       iterator
00834       lower_bound(const key_type& __k)
00835       { return _M_lower_bound(_M_begin(), _M_end(), __k); }
00836 
00837       const_iterator
00838       lower_bound(const key_type& __k) const
00839       { return _M_lower_bound(_M_begin(), _M_end(), __k); }
00840 
00841       iterator
00842       upper_bound(const key_type& __k)
00843       { return _M_upper_bound(_M_begin(), _M_end(), __k); }
00844 
00845       const_iterator
00846       upper_bound(const key_type& __k) const
00847       { return _M_upper_bound(_M_begin(), _M_end(), __k); }
00848 
00849       pair<iterator, iterator>
00850       equal_range(const key_type& __k);
00851 
00852       pair<const_iterator, const_iterator>
00853       equal_range(const key_type& __k) const;
00854 
00855       // Debugging.
00856       bool
00857       __rb_verify() const;
00858     };
00859 
00860   template<typename _Key, typename _Val, typename _KeyOfValue,
00861            typename _Compare, typename _Alloc>
00862     inline bool
00863     operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00864            const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00865     {
00866       return __x.size() == __y.size()
00867          && std::equal(__x.begin(), __x.end(), __y.begin());
00868     }
00869 
00870   template<typename _Key, typename _Val, typename _KeyOfValue,
00871            typename _Compare, typename _Alloc>
00872     inline bool
00873     operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00874           const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00875     {
00876       return std::lexicographical_compare(__x.begin(), __x.end(), 
00877                       __y.begin(), __y.end());
00878     }
00879 
00880   template<typename _Key, typename _Val, typename _KeyOfValue,
00881            typename _Compare, typename _Alloc>
00882     inline bool
00883     operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00884            const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00885     { return !(__x == __y); }
00886 
00887   template<typename _Key, typename _Val, typename _KeyOfValue,
00888            typename _Compare, typename _Alloc>
00889     inline bool
00890     operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00891           const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00892     { return __y < __x; }
00893 
00894   template<typename _Key, typename _Val, typename _KeyOfValue,
00895            typename _Compare, typename _Alloc>
00896     inline bool
00897     operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00898            const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00899     { return !(__y < __x); }
00900 
00901   template<typename _Key, typename _Val, typename _KeyOfValue,
00902            typename _Compare, typename _Alloc>
00903     inline bool
00904     operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00905            const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00906     { return !(__x < __y); }
00907 
00908   template<typename _Key, typename _Val, typename _KeyOfValue,
00909            typename _Compare, typename _Alloc>
00910     inline void
00911     swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
00912      _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
00913     { __x.swap(__y); }
00914 
00915 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00916   template<typename _Key, typename _Val, typename _KeyOfValue,
00917            typename _Compare, typename _Alloc>
00918     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
00919     _Rb_tree(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&& __x)
00920     : _M_impl(__x._M_impl._M_key_compare,
00921           std::move(__x._M_get_Node_allocator()))
00922     {
00923       if (__x._M_root() != 0)
00924     {
00925       _M_root() = __x._M_root();
00926       _M_leftmost() = __x._M_leftmost();
00927       _M_rightmost() = __x._M_rightmost();
00928       _M_root()->_M_parent = _M_end();
00929 
00930       __x._M_root() = 0;
00931       __x._M_leftmost() = __x._M_end();
00932       __x._M_rightmost() = __x._M_end();
00933 
00934       this->_M_impl._M_node_count = __x._M_impl._M_node_count;
00935       __x._M_impl._M_node_count = 0;
00936     }
00937     }
00938 #endif
00939 
00940   template<typename _Key, typename _Val, typename _KeyOfValue,
00941            typename _Compare, typename _Alloc>
00942     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
00943     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
00944     operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
00945     {
00946       if (this != &__x)
00947     {
00948       // Note that _Key may be a constant type.
00949       clear();
00950       _M_impl._M_key_compare = __x._M_impl._M_key_compare;
00951       if (__x._M_root() != 0)
00952         {
00953           _M_root() = _M_copy(__x._M_begin(), _M_end());
00954           _M_leftmost() = _S_minimum(_M_root());
00955           _M_rightmost() = _S_maximum(_M_root());
00956           _M_impl._M_node_count = __x._M_impl._M_node_count;
00957         }
00958     }
00959       return *this;
00960     }
00961 
00962   template<typename _Key, typename _Val, typename _KeyOfValue,
00963            typename _Compare, typename _Alloc>
00964 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00965     template<typename _Arg>
00966 #endif
00967     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
00968     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
00969 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00970     _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __p, _Arg&& __v)
00971 #else
00972     _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __p, const _Val& __v)
00973 #endif
00974     {
00975       bool __insert_left = (__x != 0 || __p == _M_end()
00976                 || _M_impl._M_key_compare(_KeyOfValue()(__v), 
00977                               _S_key(__p)));
00978 
00979       _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
00980 
00981       _Rb_tree_insert_and_rebalance(__insert_left, __z,
00982                     const_cast<_Base_ptr>(__p),  
00983                     this->_M_impl._M_header);
00984       ++_M_impl._M_node_count;
00985       return iterator(__z);
00986     }
00987 
00988   template<typename _Key, typename _Val, typename _KeyOfValue,
00989            typename _Compare, typename _Alloc>
00990 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00991     template<typename _Arg>
00992 #endif
00993     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
00994     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
00995 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00996     _M_insert_lower(_Base_ptr __x, _Base_ptr __p, _Arg&& __v)
00997 #else
00998     _M_insert_lower(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
00999 #endif
01000     {
01001       bool __insert_left = (__x != 0 || __p == _M_end()
01002                 || !_M_impl._M_key_compare(_S_key(__p),
01003                                _KeyOfValue()(__v)));
01004 
01005       _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
01006 
01007       _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,  
01008                     this->_M_impl._M_header);
01009       ++_M_impl._M_node_count;
01010       return iterator(__z);
01011     }
01012 
01013   template<typename _Key, typename _Val, typename _KeyOfValue,
01014            typename _Compare, typename _Alloc>
01015 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01016     template<typename _Arg>
01017 #endif
01018     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01019     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01020 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01021     _M_insert_equal_lower(_Arg&& __v)
01022 #else
01023     _M_insert_equal_lower(const _Val& __v)
01024 #endif
01025     {
01026       _Link_type __x = _M_begin();
01027       _Link_type __y = _M_end();
01028       while (__x != 0)
01029     {
01030       __y = __x;
01031       __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
01032             _S_left(__x) : _S_right(__x);
01033     }
01034       return _M_insert_lower(__x, __y, _GLIBCXX_FORWARD(_Arg, __v));
01035     }
01036 
01037   template<typename _Key, typename _Val, typename _KoV,
01038            typename _Compare, typename _Alloc>
01039     typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
01040     _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
01041     _M_copy(_Const_Link_type __x, _Link_type __p)
01042     {
01043       // Structural copy.  __x and __p must be non-null.
01044       _Link_type __top = _M_clone_node(__x);
01045       __top->_M_parent = __p;
01046 
01047       __try
01048     {
01049       if (__x->_M_right)
01050         __top->_M_right = _M_copy(_S_right(__x), __top);
01051       __p = __top;
01052       __x = _S_left(__x);
01053 
01054       while (__x != 0)
01055         {
01056           _Link_type __y = _M_clone_node(__x);
01057           __p->_M_left = __y;
01058           __y->_M_parent = __p;
01059           if (__x->_M_right)
01060         __y->_M_right = _M_copy(_S_right(__x), __y);
01061           __p = __y;
01062           __x = _S_left(__x);
01063         }
01064     }
01065       __catch(...)
01066     {
01067       _M_erase(__top);
01068       __throw_exception_again;
01069     }
01070       return __top;
01071     }
01072 
01073   template<typename _Key, typename _Val, typename _KeyOfValue,
01074            typename _Compare, typename _Alloc>
01075     void
01076     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01077     _M_erase(_Link_type __x)
01078     {
01079       // Erase without rebalancing.
01080       while (__x != 0)
01081     {
01082       _M_erase(_S_right(__x));
01083       _Link_type __y = _S_left(__x);
01084       _M_destroy_node(__x);
01085       __x = __y;
01086     }
01087     }
01088 
01089   template<typename _Key, typename _Val, typename _KeyOfValue,
01090            typename _Compare, typename _Alloc>
01091     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01092               _Compare, _Alloc>::iterator
01093     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01094     _M_lower_bound(_Link_type __x, _Link_type __y,
01095            const _Key& __k)
01096     {
01097       while (__x != 0)
01098     if (!_M_impl._M_key_compare(_S_key(__x), __k))
01099       __y = __x, __x = _S_left(__x);
01100     else
01101       __x = _S_right(__x);
01102       return iterator(__y);
01103     }
01104 
01105   template<typename _Key, typename _Val, typename _KeyOfValue,
01106            typename _Compare, typename _Alloc>
01107     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01108               _Compare, _Alloc>::const_iterator
01109     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01110     _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
01111            const _Key& __k) const
01112     {
01113       while (__x != 0)
01114     if (!_M_impl._M_key_compare(_S_key(__x), __k))
01115       __y = __x, __x = _S_left(__x);
01116     else
01117       __x = _S_right(__x);
01118       return const_iterator(__y);
01119     }
01120 
01121   template<typename _Key, typename _Val, typename _KeyOfValue,
01122            typename _Compare, typename _Alloc>
01123     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01124               _Compare, _Alloc>::iterator
01125     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01126     _M_upper_bound(_Link_type __x, _Link_type __y,
01127            const _Key& __k)
01128     {
01129       while (__x != 0)
01130     if (_M_impl._M_key_compare(__k, _S_key(__x)))
01131       __y = __x, __x = _S_left(__x);
01132     else
01133       __x = _S_right(__x);
01134       return iterator(__y);
01135     }
01136 
01137   template<typename _Key, typename _Val, typename _KeyOfValue,
01138            typename _Compare, typename _Alloc>
01139     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01140               _Compare, _Alloc>::const_iterator
01141     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01142     _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
01143            const _Key& __k) const
01144     {
01145       while (__x != 0)
01146     if (_M_impl._M_key_compare(__k, _S_key(__x)))
01147       __y = __x, __x = _S_left(__x);
01148     else
01149       __x = _S_right(__x);
01150       return const_iterator(__y);
01151     }
01152 
01153   template<typename _Key, typename _Val, typename _KeyOfValue,
01154            typename _Compare, typename _Alloc>
01155     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01156                _Compare, _Alloc>::iterator,
01157      typename _Rb_tree<_Key, _Val, _KeyOfValue,
01158                _Compare, _Alloc>::iterator>
01159     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01160     equal_range(const _Key& __k)
01161     {
01162       _Link_type __x = _M_begin();
01163       _Link_type __y = _M_end();
01164       while (__x != 0)
01165     {
01166       if (_M_impl._M_key_compare(_S_key(__x), __k))
01167         __x = _S_right(__x);
01168       else if (_M_impl._M_key_compare(__k, _S_key(__x)))
01169         __y = __x, __x = _S_left(__x);
01170       else
01171         {
01172           _Link_type __xu(__x), __yu(__y);
01173           __y = __x, __x = _S_left(__x);
01174           __xu = _S_right(__xu);
01175           return pair<iterator,
01176                   iterator>(_M_lower_bound(__x, __y, __k),
01177                     _M_upper_bound(__xu, __yu, __k));
01178         }
01179     }
01180       return pair<iterator, iterator>(iterator(__y),
01181                       iterator(__y));
01182     }
01183 
01184   template<typename _Key, typename _Val, typename _KeyOfValue,
01185            typename _Compare, typename _Alloc>
01186     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01187                _Compare, _Alloc>::const_iterator,
01188      typename _Rb_tree<_Key, _Val, _KeyOfValue,
01189                _Compare, _Alloc>::const_iterator>
01190     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01191     equal_range(const _Key& __k) const
01192     {
01193       _Const_Link_type __x = _M_begin();
01194       _Const_Link_type __y = _M_end();
01195       while (__x != 0)
01196     {
01197       if (_M_impl._M_key_compare(_S_key(__x), __k))
01198         __x = _S_right(__x);
01199       else if (_M_impl._M_key_compare(__k, _S_key(__x)))
01200         __y = __x, __x = _S_left(__x);
01201       else
01202         {
01203           _Const_Link_type __xu(__x), __yu(__y);
01204           __y = __x, __x = _S_left(__x);
01205           __xu = _S_right(__xu);
01206           return pair<const_iterator,
01207                   const_iterator>(_M_lower_bound(__x, __y, __k),
01208                       _M_upper_bound(__xu, __yu, __k));
01209         }
01210     }
01211       return pair<const_iterator, const_iterator>(const_iterator(__y),
01212                           const_iterator(__y));
01213     }
01214 
01215   template<typename _Key, typename _Val, typename _KeyOfValue,
01216            typename _Compare, typename _Alloc>
01217     void
01218     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01219     swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
01220     {
01221       if (_M_root() == 0)
01222     {
01223       if (__t._M_root() != 0)
01224         {
01225           _M_root() = __t._M_root();
01226           _M_leftmost() = __t._M_leftmost();
01227           _M_rightmost() = __t._M_rightmost();
01228           _M_root()->_M_parent = _M_end();
01229           
01230           __t._M_root() = 0;
01231           __t._M_leftmost() = __t._M_end();
01232           __t._M_rightmost() = __t._M_end();
01233         }
01234     }
01235       else if (__t._M_root() == 0)
01236     {
01237       __t._M_root() = _M_root();
01238       __t._M_leftmost() = _M_leftmost();
01239       __t._M_rightmost() = _M_rightmost();
01240       __t._M_root()->_M_parent = __t._M_end();
01241       
01242       _M_root() = 0;
01243       _M_leftmost() = _M_end();
01244       _M_rightmost() = _M_end();
01245     }
01246       else
01247     {
01248       std::swap(_M_root(),__t._M_root());
01249       std::swap(_M_leftmost(),__t._M_leftmost());
01250       std::swap(_M_rightmost(),__t._M_rightmost());
01251       
01252       _M_root()->_M_parent = _M_end();
01253       __t._M_root()->_M_parent = __t._M_end();
01254     }
01255       // No need to swap header's color as it does not change.
01256       std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
01257       std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
01258       
01259       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01260       // 431. Swapping containers with unequal allocators.
01261       std::__alloc_swap<_Node_allocator>::
01262     _S_do_it(_M_get_Node_allocator(), __t._M_get_Node_allocator());
01263     }
01264 
01265   template<typename _Key, typename _Val, typename _KeyOfValue,
01266            typename _Compare, typename _Alloc>
01267 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01268     template<typename _Arg>
01269 #endif
01270     pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
01271                _Compare, _Alloc>::iterator, bool>
01272     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01274     _M_insert_unique(_Arg&& __v)
01275 #else
01276     _M_insert_unique(const _Val& __v)
01277 #endif
01278     {
01279       _Link_type __x = _M_begin();
01280       _Link_type __y = _M_end();
01281       bool __comp = true;
01282       while (__x != 0)
01283     {
01284       __y = __x;
01285       __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
01286       __x = __comp ? _S_left(__x) : _S_right(__x);
01287     }
01288       iterator __j = iterator(__y);
01289       if (__comp)
01290     {
01291       if (__j == begin())
01292         return pair<iterator, bool>
01293           (_M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v)), true);
01294       else
01295         --__j;
01296     }
01297       if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
01298     return pair<iterator, bool>
01299       (_M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v)), true);
01300       return pair<iterator, bool>(__j, false);
01301     }
01302 
01303   template<typename _Key, typename _Val, typename _KeyOfValue,
01304            typename _Compare, typename _Alloc>
01305 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01306     template<typename _Arg>
01307 #endif
01308     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01309     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01310 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01311     _M_insert_equal(_Arg&& __v)
01312 #else
01313     _M_insert_equal(const _Val& __v)
01314 #endif
01315     {
01316       _Link_type __x = _M_begin();
01317       _Link_type __y = _M_end();
01318       while (__x != 0)
01319     {
01320       __y = __x;
01321       __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
01322             _S_left(__x) : _S_right(__x);
01323     }
01324       return _M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v));
01325     }
01326 
01327   template<typename _Key, typename _Val, typename _KeyOfValue,
01328            typename _Compare, typename _Alloc>
01329 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01330     template<typename _Arg>
01331 #endif
01332     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01333     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01335     _M_insert_unique_(const_iterator __position, _Arg&& __v)
01336 #else
01337     _M_insert_unique_(const_iterator __position, const _Val& __v)
01338 #endif
01339     {
01340       // end()
01341       if (__position._M_node == _M_end())
01342     {
01343       if (size() > 0
01344           && _M_impl._M_key_compare(_S_key(_M_rightmost()), 
01345                     _KeyOfValue()(__v)))
01346         return _M_insert_(0, _M_rightmost(), _GLIBCXX_FORWARD(_Arg, __v));
01347       else
01348         return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
01349     }
01350       else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
01351                       _S_key(__position._M_node)))
01352     {
01353       // First, try before...
01354       const_iterator __before = __position;
01355       if (__position._M_node == _M_leftmost()) // begin()
01356         return _M_insert_(_M_leftmost(), _M_leftmost(),
01357                   _GLIBCXX_FORWARD(_Arg, __v));
01358       else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), 
01359                       _KeyOfValue()(__v)))
01360         {
01361           if (_S_right(__before._M_node) == 0)
01362         return _M_insert_(0, __before._M_node,
01363                   _GLIBCXX_FORWARD(_Arg, __v));
01364           else
01365         return _M_insert_(__position._M_node,
01366                   __position._M_node,
01367                   _GLIBCXX_FORWARD(_Arg, __v));
01368         }
01369       else
01370         return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
01371     }
01372       else if (_M_impl._M_key_compare(_S_key(__position._M_node),
01373                       _KeyOfValue()(__v)))
01374     {
01375       // ... then try after.
01376       const_iterator __after = __position;
01377       if (__position._M_node == _M_rightmost())
01378         return _M_insert_(0, _M_rightmost(),
01379                   _GLIBCXX_FORWARD(_Arg, __v));
01380       else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
01381                       _S_key((++__after)._M_node)))
01382         {
01383           if (_S_right(__position._M_node) == 0)
01384         return _M_insert_(0, __position._M_node,
01385                   _GLIBCXX_FORWARD(_Arg, __v));
01386           else
01387         return _M_insert_(__after._M_node, __after._M_node,
01388                   _GLIBCXX_FORWARD(_Arg, __v));
01389         }
01390       else
01391         return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
01392     }
01393       else
01394     // Equivalent keys.
01395     return __position._M_const_cast();
01396     }
01397 
01398   template<typename _Key, typename _Val, typename _KeyOfValue,
01399            typename _Compare, typename _Alloc>
01400 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01401     template<typename _Arg>
01402 #endif
01403     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
01404     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01405 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01406     _M_insert_equal_(const_iterator __position, _Arg&& __v)
01407 #else
01408     _M_insert_equal_(const_iterator __position, const _Val& __v)
01409 #endif
01410     {
01411       // end()
01412       if (__position._M_node == _M_end())
01413     {
01414       if (size() > 0
01415           && !_M_impl._M_key_compare(_KeyOfValue()(__v),
01416                      _S_key(_M_rightmost())))
01417         return _M_insert_(0, _M_rightmost(),
01418                   _GLIBCXX_FORWARD(_Arg, __v));
01419       else
01420         return _M_insert_equal(_GLIBCXX_FORWARD(_Arg, __v));
01421     }
01422       else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
01423                        _KeyOfValue()(__v)))
01424     {
01425       // First, try before...
01426       const_iterator __before = __position;
01427       if (__position._M_node == _M_leftmost()) // begin()
01428         return _M_insert_(_M_leftmost(), _M_leftmost(),
01429                   _GLIBCXX_FORWARD(_Arg, __v));
01430       else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
01431                        _S_key((--__before)._M_node)))
01432         {
01433           if (_S_right(__before._M_node) == 0)
01434         return _M_insert_(0, __before._M_node,
01435                   _GLIBCXX_FORWARD(_Arg, __v));
01436           else
01437         return _M_insert_(__position._M_node,
01438                   __position._M_node,
01439                   _GLIBCXX_FORWARD(_Arg, __v));
01440         }
01441       else
01442         return _M_insert_equal(_GLIBCXX_FORWARD(_Arg, __v));
01443     }
01444       else
01445     {
01446       // ... then try after.  
01447       const_iterator __after = __position;
01448       if (__position._M_node == _M_rightmost())
01449         return _M_insert_(0, _M_rightmost(),
01450                   _GLIBCXX_FORWARD(_Arg, __v));
01451       else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
01452                        _KeyOfValue()(__v)))
01453         {
01454           if (_S_right(__position._M_node) == 0)
01455         return _M_insert_(0, __position._M_node,
01456                   _GLIBCXX_FORWARD(_Arg, __v));
01457           else
01458         return _M_insert_(__after._M_node, __after._M_node,
01459                   _GLIBCXX_FORWARD(_Arg, __v));
01460         }
01461       else
01462         return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
01463     }
01464     }
01465 
01466   template<typename _Key, typename _Val, typename _KoV,
01467            typename _Cmp, typename _Alloc>
01468     template<class _II>
01469       void
01470       _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
01471       _M_insert_unique(_II __first, _II __last)
01472       {
01473     for (; __first != __last; ++__first)
01474       _M_insert_unique_(end(), *__first);
01475       }
01476 
01477   template<typename _Key, typename _Val, typename _KoV,
01478            typename _Cmp, typename _Alloc>
01479     template<class _II>
01480       void
01481       _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
01482       _M_insert_equal(_II __first, _II __last)
01483       {
01484     for (; __first != __last; ++__first)
01485       _M_insert_equal_(end(), *__first);
01486       }
01487 
01488   template<typename _Key, typename _Val, typename _KeyOfValue,
01489            typename _Compare, typename _Alloc>
01490     void
01491     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01492     _M_erase_aux(const_iterator __position)
01493     {
01494       _Link_type __y =
01495     static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
01496                 (const_cast<_Base_ptr>(__position._M_node),
01497                  this->_M_impl._M_header));
01498       _M_destroy_node(__y);
01499       --_M_impl._M_node_count;
01500     }
01501 
01502   template<typename _Key, typename _Val, typename _KeyOfValue,
01503            typename _Compare, typename _Alloc>
01504     void
01505     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01506     _M_erase_aux(const_iterator __first, const_iterator __last)
01507     {
01508       if (__first == begin() && __last == end())
01509     clear();
01510       else
01511     while (__first != __last)
01512       erase(__first++);
01513     }
01514 
01515   template<typename _Key, typename _Val, typename _KeyOfValue,
01516            typename _Compare, typename _Alloc>
01517     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
01518     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01519     erase(const _Key& __x)
01520     {
01521       pair<iterator, iterator> __p = equal_range(__x);
01522       const size_type __old_size = size();
01523       erase(__p.first, __p.second);
01524       return __old_size - size();
01525     }
01526 
01527   template<typename _Key, typename _Val, typename _KeyOfValue,
01528            typename _Compare, typename _Alloc>
01529     void
01530     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01531     erase(const _Key* __first, const _Key* __last)
01532     {
01533       while (__first != __last)
01534     erase(*__first++);
01535     }
01536 
01537   template<typename _Key, typename _Val, typename _KeyOfValue,
01538            typename _Compare, typename _Alloc>
01539     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01540               _Compare, _Alloc>::iterator
01541     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01542     find(const _Key& __k)
01543     {
01544       iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
01545       return (__j == end()
01546           || _M_impl._M_key_compare(__k,
01547                     _S_key(__j._M_node))) ? end() : __j;
01548     }
01549 
01550   template<typename _Key, typename _Val, typename _KeyOfValue,
01551            typename _Compare, typename _Alloc>
01552     typename _Rb_tree<_Key, _Val, _KeyOfValue,
01553               _Compare, _Alloc>::const_iterator
01554     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01555     find(const _Key& __k) const
01556     {
01557       const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
01558       return (__j == end()
01559           || _M_impl._M_key_compare(__k, 
01560                     _S_key(__j._M_node))) ? end() : __j;
01561     }
01562 
01563   template<typename _Key, typename _Val, typename _KeyOfValue,
01564            typename _Compare, typename _Alloc>
01565     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
01566     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
01567     count(const _Key& __k) const
01568     {
01569       pair<const_iterator, const_iterator> __p = equal_range(__k);
01570       const size_type __n = std::distance(__p.first, __p.second);
01571       return __n;
01572     }
01573 
01574   _GLIBCXX_PURE unsigned int
01575   _Rb_tree_black_count(const _Rb_tree_node_base* __node,
01576                        const _Rb_tree_node_base* __root) throw ();
01577 
01578   template<typename _Key, typename _Val, typename _KeyOfValue,
01579            typename _Compare, typename _Alloc>
01580     bool
01581     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
01582     {
01583       if (_M_impl._M_node_count == 0 || begin() == end())
01584     return _M_impl._M_node_count == 0 && begin() == end()
01585            && this->_M_impl._M_header._M_left == _M_end()
01586            && this->_M_impl._M_header._M_right == _M_end();
01587 
01588       unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
01589       for (const_iterator __it = begin(); __it != end(); ++__it)
01590     {
01591       _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
01592       _Const_Link_type __L = _S_left(__x);
01593       _Const_Link_type __R = _S_right(__x);
01594 
01595       if (__x->_M_color == _S_red)
01596         if ((__L && __L->_M_color == _S_red)
01597         || (__R && __R->_M_color == _S_red))
01598           return false;
01599 
01600       if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
01601         return false;
01602       if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
01603         return false;
01604 
01605       if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
01606         return false;
01607     }
01608 
01609       if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
01610     return false;
01611       if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
01612     return false;
01613       return true;
01614     }
01615 
01616 _GLIBCXX_END_NAMESPACE_VERSION
01617 } // namespace
01618 
01619 #endif