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