Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

stl_tree.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1996,1997
00004  * Silicon Graphics Computer Systems, Inc.
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Silicon Graphics makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1994
00016  * Hewlett-Packard Company
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Hewlett-Packard Company makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  *
00026  *
00027  */
00028 
00029 /* NOTE: This is an internal header file, included by other STL headers.
00030  *   You should not attempt to use it directly.
00031  */
00032 
00033 #ifndef __SGI_STL_INTERNAL_TREE_H
00034 #define __SGI_STL_INTERNAL_TREE_H
00035 
00036 /*
00037 
00038 Red-black tree class, designed for use in implementing STL
00039 associative containers (set, multiset, map, and multimap). The
00040 insertion and deletion algorithms are based on those in Cormen,
00041 Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
00042 except that
00043 
00044 (1) the header cell is maintained with links not only to the root
00045 but also to the leftmost node of the tree, to enable constant time
00046 begin(), and to the rightmost node of the tree, to enable linear time
00047 performance when used with the generic set algorithms (set_union,
00048 etc.);
00049 
00050 (2) when a node being deleted has two children its successor node is
00051 relinked into its place, rather than copied, so that the only
00052 iterators invalidated are those referring to the deleted node.
00053 
00054 */
00055 
00056 #include <bits/stl_algobase.h>
00057 #include <bits/stl_alloc.h>
00058 #include <bits/stl_construct.h>
00059 #include <bits/stl_function.h>
00060 
00061 namespace std
00062 { 
00063 
00064 typedef bool _Rb_tree_Color_type;
00065 const _Rb_tree_Color_type _S_rb_tree_red = false;
00066 const _Rb_tree_Color_type _S_rb_tree_black = true;
00067 
00068 struct _Rb_tree_node_base
00069 {
00070   typedef _Rb_tree_Color_type _Color_type;
00071   typedef _Rb_tree_node_base* _Base_ptr;
00072 
00073   _Color_type _M_color; 
00074   _Base_ptr _M_parent;
00075   _Base_ptr _M_left;
00076   _Base_ptr _M_right;
00077 
00078   static _Base_ptr _S_minimum(_Base_ptr __x)
00079   {
00080     while (__x->_M_left != 0) __x = __x->_M_left;
00081     return __x;
00082   }
00083 
00084   static _Base_ptr _S_maximum(_Base_ptr __x)
00085   {
00086     while (__x->_M_right != 0) __x = __x->_M_right;
00087     return __x;
00088   }
00089 };
00090 
00091 template <class _Value>
00092 struct _Rb_tree_node : public _Rb_tree_node_base
00093 {
00094   typedef _Rb_tree_node<_Value>* _Link_type;
00095   _Value _M_value_field;
00096 };
00097 
00098 
00099 struct _Rb_tree_base_iterator
00100 {
00101   typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
00102   typedef bidirectional_iterator_tag iterator_category;
00103   typedef ptrdiff_t difference_type;
00104   _Base_ptr _M_node;
00105 
00106   void _M_increment()
00107   {
00108     if (_M_node->_M_right != 0) {
00109       _M_node = _M_node->_M_right;
00110       while (_M_node->_M_left != 0)
00111         _M_node = _M_node->_M_left;
00112     }
00113     else {
00114       _Base_ptr __y = _M_node->_M_parent;
00115       while (_M_node == __y->_M_right) {
00116         _M_node = __y;
00117         __y = __y->_M_parent;
00118       }
00119       if (_M_node->_M_right != __y)
00120         _M_node = __y;
00121     }
00122   }
00123 
00124   void _M_decrement()
00125   {
00126     if (_M_node->_M_color == _S_rb_tree_red &&
00127         _M_node->_M_parent->_M_parent == _M_node)
00128       _M_node = _M_node->_M_right;
00129     else if (_M_node->_M_left != 0) {
00130       _Base_ptr __y = _M_node->_M_left;
00131       while (__y->_M_right != 0)
00132         __y = __y->_M_right;
00133       _M_node = __y;
00134     }
00135     else {
00136       _Base_ptr __y = _M_node->_M_parent;
00137       while (_M_node == __y->_M_left) {
00138         _M_node = __y;
00139         __y = __y->_M_parent;
00140       }
00141       _M_node = __y;
00142     }
00143   }
00144 };
00145 
00146 template <class _Value, class _Ref, class _Ptr>
00147 struct _Rb_tree_iterator : public _Rb_tree_base_iterator
00148 {
00149   typedef _Value value_type;
00150   typedef _Ref reference;
00151   typedef _Ptr pointer;
00152   typedef _Rb_tree_iterator<_Value, _Value&, _Value*>             
00153     iterator;
00154   typedef _Rb_tree_iterator<_Value, const _Value&, const _Value*> 
00155     const_iterator;
00156   typedef _Rb_tree_iterator<_Value, _Ref, _Ptr>                   
00157     _Self;
00158   typedef _Rb_tree_node<_Value>* _Link_type;
00159 
00160   _Rb_tree_iterator() {}
00161   _Rb_tree_iterator(_Link_type __x) { _M_node = __x; }
00162   _Rb_tree_iterator(const iterator& __it) { _M_node = __it._M_node; }
00163 
00164   reference operator*() const { return _Link_type(_M_node)->_M_value_field; }
00165   pointer operator->() const { return &(operator*()); }
00166 
00167   _Self& operator++() { _M_increment(); return *this; }
00168   _Self operator++(int) {
00169     _Self __tmp = *this;
00170     _M_increment();
00171     return __tmp;
00172   }
00173     
00174   _Self& operator--() { _M_decrement(); return *this; }
00175   _Self operator--(int) {
00176     _Self __tmp = *this;
00177     _M_decrement();
00178     return __tmp;
00179   }
00180 };
00181 
00182 template <class _Value, class _Ref, class _Ptr>
00183 inline bool operator==(const _Rb_tree_iterator<_Value, _Ref, _Ptr>& __x,
00184                const _Rb_tree_iterator<_Value, _Ref, _Ptr>& __y) {
00185   return __x._M_node == __y._M_node;
00186 }
00187 
00188 template <class _Value>
00189 inline bool operator==(const _Rb_tree_iterator<_Value, const _Value&, const _Value*>& __x,
00190                const _Rb_tree_iterator<_Value, _Value&, _Value*>& __y) {
00191   return __x._M_node == __y._M_node;
00192 }
00193 
00194 template <class _Value>
00195 inline bool operator==(const _Rb_tree_iterator<_Value, _Value&, _Value*>& __x,
00196                const _Rb_tree_iterator<_Value, const _Value&, const _Value*>& __y) {
00197   return __x._M_node == __y._M_node;
00198 }
00199 
00200 template <class _Value, class _Ref, class _Ptr>
00201 inline bool operator!=(const _Rb_tree_iterator<_Value, _Ref, _Ptr>& __x,
00202                const _Rb_tree_iterator<_Value, _Ref, _Ptr>& __y) {
00203   return __x._M_node != __y._M_node;
00204 }
00205 
00206 template <class _Value>
00207 inline bool operator!=(const _Rb_tree_iterator<_Value, const _Value&, const _Value*>& __x,
00208                const _Rb_tree_iterator<_Value, _Value&, _Value*>& __y) {
00209   return __x._M_node != __y._M_node;
00210 }
00211 
00212 template <class _Value>
00213 inline bool operator!=(const _Rb_tree_iterator<_Value, _Value&, _Value*>& __x,
00214                const _Rb_tree_iterator<_Value, const _Value&, const _Value*>& __y) {
00215   return __x._M_node != __y._M_node;
00216 }
00217 
00218 inline void 
00219 _Rb_tree_rotate_left(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
00220 {
00221   _Rb_tree_node_base* __y = __x->_M_right;
00222   __x->_M_right = __y->_M_left;
00223   if (__y->_M_left !=0)
00224     __y->_M_left->_M_parent = __x;
00225   __y->_M_parent = __x->_M_parent;
00226 
00227   if (__x == __root)
00228     __root = __y;
00229   else if (__x == __x->_M_parent->_M_left)
00230     __x->_M_parent->_M_left = __y;
00231   else
00232     __x->_M_parent->_M_right = __y;
00233   __y->_M_left = __x;
00234   __x->_M_parent = __y;
00235 }
00236 
00237 inline void 
00238 _Rb_tree_rotate_right(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
00239 {
00240   _Rb_tree_node_base* __y = __x->_M_left;
00241   __x->_M_left = __y->_M_right;
00242   if (__y->_M_right != 0)
00243     __y->_M_right->_M_parent = __x;
00244   __y->_M_parent = __x->_M_parent;
00245 
00246   if (__x == __root)
00247     __root = __y;
00248   else if (__x == __x->_M_parent->_M_right)
00249     __x->_M_parent->_M_right = __y;
00250   else
00251     __x->_M_parent->_M_left = __y;
00252   __y->_M_right = __x;
00253   __x->_M_parent = __y;
00254 }
00255 
00256 inline void 
00257 _Rb_tree_rebalance(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
00258 {
00259   __x->_M_color = _S_rb_tree_red;
00260   while (__x != __root && __x->_M_parent->_M_color == _S_rb_tree_red) {
00261     if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) {
00262       _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
00263       if (__y && __y->_M_color == _S_rb_tree_red) {
00264         __x->_M_parent->_M_color = _S_rb_tree_black;
00265         __y->_M_color = _S_rb_tree_black;
00266         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
00267         __x = __x->_M_parent->_M_parent;
00268       }
00269       else {
00270         if (__x == __x->_M_parent->_M_right) {
00271           __x = __x->_M_parent;
00272           _Rb_tree_rotate_left(__x, __root);
00273         }
00274         __x->_M_parent->_M_color = _S_rb_tree_black;
00275         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
00276         _Rb_tree_rotate_right(__x->_M_parent->_M_parent, __root);
00277       }
00278     }
00279     else {
00280       _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
00281       if (__y && __y->_M_color == _S_rb_tree_red) {
00282         __x->_M_parent->_M_color = _S_rb_tree_black;
00283         __y->_M_color = _S_rb_tree_black;
00284         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
00285         __x = __x->_M_parent->_M_parent;
00286       }
00287       else {
00288         if (__x == __x->_M_parent->_M_left) {
00289           __x = __x->_M_parent;
00290           _Rb_tree_rotate_right(__x, __root);
00291         }
00292         __x->_M_parent->_M_color = _S_rb_tree_black;
00293         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
00294         _Rb_tree_rotate_left(__x->_M_parent->_M_parent, __root);
00295       }
00296     }
00297   }
00298   __root->_M_color = _S_rb_tree_black;
00299 }
00300 
00301 inline _Rb_tree_node_base*
00302 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* __z,
00303                              _Rb_tree_node_base*& __root,
00304                              _Rb_tree_node_base*& __leftmost,
00305                              _Rb_tree_node_base*& __rightmost)
00306 {
00307   _Rb_tree_node_base* __y = __z;
00308   _Rb_tree_node_base* __x = 0;
00309   _Rb_tree_node_base* __x_parent = 0;
00310   if (__y->_M_left == 0)     // __z has at most one non-null child. y == z.
00311     __x = __y->_M_right;     // __x might be null.
00312   else
00313     if (__y->_M_right == 0)  // __z has exactly one non-null child. y == z.
00314       __x = __y->_M_left;    // __x is not null.
00315     else {                   // __z has two non-null children.  Set __y to
00316       __y = __y->_M_right;   //   __z's successor.  __x might be null.
00317       while (__y->_M_left != 0)
00318         __y = __y->_M_left;
00319       __x = __y->_M_right;
00320     }
00321   if (__y != __z) {          // relink y in place of z.  y is z's successor
00322     __z->_M_left->_M_parent = __y; 
00323     __y->_M_left = __z->_M_left;
00324     if (__y != __z->_M_right) {
00325       __x_parent = __y->_M_parent;
00326       if (__x) __x->_M_parent = __y->_M_parent;
00327       __y->_M_parent->_M_left = __x;      // __y must be a child of _M_left
00328       __y->_M_right = __z->_M_right;
00329       __z->_M_right->_M_parent = __y;
00330     }
00331     else
00332       __x_parent = __y;  
00333     if (__root == __z)
00334       __root = __y;
00335     else if (__z->_M_parent->_M_left == __z)
00336       __z->_M_parent->_M_left = __y;
00337     else 
00338       __z->_M_parent->_M_right = __y;
00339     __y->_M_parent = __z->_M_parent;
00340     std::swap(__y->_M_color, __z->_M_color);
00341     __y = __z;
00342     // __y now points to node to be actually deleted
00343   }
00344   else {                        // __y == __z
00345     __x_parent = __y->_M_parent;
00346     if (__x) __x->_M_parent = __y->_M_parent;   
00347     if (__root == __z)
00348       __root = __x;
00349     else 
00350       if (__z->_M_parent->_M_left == __z)
00351         __z->_M_parent->_M_left = __x;
00352       else
00353         __z->_M_parent->_M_right = __x;
00354     if (__leftmost == __z) 
00355       if (__z->_M_right == 0)        // __z->_M_left must be null also
00356         __leftmost = __z->_M_parent;
00357     // makes __leftmost == _M_header if __z == __root
00358       else
00359         __leftmost = _Rb_tree_node_base::_S_minimum(__x);
00360     if (__rightmost == __z)  
00361       if (__z->_M_left == 0)         // __z->_M_right must be null also
00362         __rightmost = __z->_M_parent;  
00363     // makes __rightmost == _M_header if __z == __root
00364       else                      // __x == __z->_M_left
00365         __rightmost = _Rb_tree_node_base::_S_maximum(__x);
00366   }
00367   if (__y->_M_color != _S_rb_tree_red) { 
00368     while (__x != __root && (__x == 0 || __x->_M_color == _S_rb_tree_black))
00369       if (__x == __x_parent->_M_left) {
00370         _Rb_tree_node_base* __w = __x_parent->_M_right;
00371         if (__w->_M_color == _S_rb_tree_red) {
00372           __w->_M_color = _S_rb_tree_black;
00373           __x_parent->_M_color = _S_rb_tree_red;
00374           _Rb_tree_rotate_left(__x_parent, __root);
00375           __w = __x_parent->_M_right;
00376         }
00377         if ((__w->_M_left == 0 || 
00378              __w->_M_left->_M_color == _S_rb_tree_black) &&
00379             (__w->_M_right == 0 || 
00380              __w->_M_right->_M_color == _S_rb_tree_black)) {
00381           __w->_M_color = _S_rb_tree_red;
00382           __x = __x_parent;
00383           __x_parent = __x_parent->_M_parent;
00384         } else {
00385           if (__w->_M_right == 0 || 
00386               __w->_M_right->_M_color == _S_rb_tree_black) {
00387             if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
00388             __w->_M_color = _S_rb_tree_red;
00389             _Rb_tree_rotate_right(__w, __root);
00390             __w = __x_parent->_M_right;
00391           }
00392           __w->_M_color = __x_parent->_M_color;
00393           __x_parent->_M_color = _S_rb_tree_black;
00394           if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
00395           _Rb_tree_rotate_left(__x_parent, __root);
00396           break;
00397         }
00398       } else {                  // same as above, with _M_right <-> _M_left.
00399         _Rb_tree_node_base* __w = __x_parent->_M_left;
00400         if (__w->_M_color == _S_rb_tree_red) {
00401           __w->_M_color = _S_rb_tree_black;
00402           __x_parent->_M_color = _S_rb_tree_red;
00403           _Rb_tree_rotate_right(__x_parent, __root);
00404           __w = __x_parent->_M_left;
00405         }
00406         if ((__w->_M_right == 0 || 
00407              __w->_M_right->_M_color == _S_rb_tree_black) &&
00408             (__w->_M_left == 0 || 
00409              __w->_M_left->_M_color == _S_rb_tree_black)) {
00410           __w->_M_color = _S_rb_tree_red;
00411           __x = __x_parent;
00412           __x_parent = __x_parent->_M_parent;
00413         } else {
00414           if (__w->_M_left == 0 || 
00415               __w->_M_left->_M_color == _S_rb_tree_black) {
00416             if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
00417             __w->_M_color = _S_rb_tree_red;
00418             _Rb_tree_rotate_left(__w, __root);
00419             __w = __x_parent->_M_left;
00420           }
00421           __w->_M_color = __x_parent->_M_color;
00422           __x_parent->_M_color = _S_rb_tree_black;
00423           if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
00424           _Rb_tree_rotate_right(__x_parent, __root);
00425           break;
00426         }
00427       }
00428     if (__x) __x->_M_color = _S_rb_tree_black;
00429   }
00430   return __y;
00431 }
00432 
00433 // Base class to encapsulate the differences between old SGI-style
00434 // allocators and standard-conforming allocators.  In order to avoid
00435 // having an empty base class, we arbitrarily move one of rb_tree's
00436 // data members into the base class.
00437 
00438 // _Base for general standard-conforming allocators.
00439 template <class _Tp, class _Alloc, bool _S_instanceless>
00440 class _Rb_tree_alloc_base {
00441 public:
00442   typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
00443   allocator_type get_allocator() const { return _M_node_allocator; }
00444 
00445   _Rb_tree_alloc_base(const allocator_type& __a)
00446     : _M_node_allocator(__a), _M_header(0) {}
00447 
00448 protected:
00449   typename _Alloc_traits<_Rb_tree_node<_Tp>, _Alloc>::allocator_type
00450            _M_node_allocator;
00451   _Rb_tree_node<_Tp>* _M_header;
00452 
00453   _Rb_tree_node<_Tp>* _M_get_node() 
00454     { return _M_node_allocator.allocate(1); }
00455   void _M_put_node(_Rb_tree_node<_Tp>* __p) 
00456     { _M_node_allocator.deallocate(__p, 1); }
00457 };
00458 
00459 // Specialization for instanceless allocators.
00460 template <class _Tp, class _Alloc>
00461 class _Rb_tree_alloc_base<_Tp, _Alloc, true> {
00462 public:
00463   typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
00464   allocator_type get_allocator() const { return allocator_type(); }
00465 
00466   _Rb_tree_alloc_base(const allocator_type&) : _M_header(0) {}
00467 
00468 protected:
00469   _Rb_tree_node<_Tp>* _M_header;
00470 
00471   typedef typename _Alloc_traits<_Rb_tree_node<_Tp>, _Alloc>::_Alloc_type
00472           _Alloc_type;
00473 
00474   _Rb_tree_node<_Tp>* _M_get_node()
00475     { return _Alloc_type::allocate(1); }
00476   void _M_put_node(_Rb_tree_node<_Tp>* __p)
00477     { _Alloc_type::deallocate(__p, 1); }
00478 };
00479 
00480 template <class _Tp, class _Alloc>
00481 struct _Rb_tree_base
00482   : public _Rb_tree_alloc_base<_Tp, _Alloc,
00483                                _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00484 {
00485   typedef _Rb_tree_alloc_base<_Tp, _Alloc,
00486                               _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00487           _Base;
00488   typedef typename _Base::allocator_type allocator_type;
00489 
00490   _Rb_tree_base(const allocator_type& __a) 
00491     : _Base(__a) { _M_header = _M_get_node(); }
00492   ~_Rb_tree_base() { _M_put_node(_M_header); }
00493 
00494 };
00495 
00496 
00497 template <class _Key, class _Value, class _KeyOfValue, class _Compare,
00498           class _Alloc = allocator<_Value> >
00499 class _Rb_tree : protected _Rb_tree_base<_Value, _Alloc> {
00500   typedef _Rb_tree_base<_Value, _Alloc> _Base;
00501 protected:
00502   typedef _Rb_tree_node_base* _Base_ptr;
00503   typedef _Rb_tree_node<_Value> _Rb_tree_node;
00504   typedef _Rb_tree_Color_type _Color_type;
00505 public:
00506   typedef _Key key_type;
00507   typedef _Value value_type;
00508   typedef value_type* pointer;
00509   typedef const value_type* const_pointer;
00510   typedef value_type& reference;
00511   typedef const value_type& const_reference;
00512   typedef _Rb_tree_node* _Link_type;
00513   typedef size_t size_type;
00514   typedef ptrdiff_t difference_type;
00515 
00516   typedef typename _Base::allocator_type allocator_type;
00517   allocator_type get_allocator() const { return _Base::get_allocator(); }
00518 
00519 protected:
00520   using _Base::_M_get_node;
00521   using _Base::_M_put_node;
00522   using _Base::_M_header;
00523 
00524 protected:
00525 
00526   _Link_type _M_create_node(const value_type& __x)
00527   {
00528     _Link_type __tmp = _M_get_node();
00529     __STL_TRY {
00530       construct(&__tmp->_M_value_field, __x);
00531     }
00532     __STL_UNWIND(_M_put_node(__tmp));
00533     return __tmp;
00534   }
00535 
00536   _Link_type _M_clone_node(_Link_type __x)
00537   {
00538     _Link_type __tmp = _M_create_node(__x->_M_value_field);
00539     __tmp->_M_color = __x->_M_color;
00540     __tmp->_M_left = 0;
00541     __tmp->_M_right = 0;
00542     return __tmp;
00543   }
00544 
00545   void destroy_node(_Link_type __p)
00546   {
00547     destroy(&__p->_M_value_field);
00548     _M_put_node(__p);
00549   }
00550 
00551 protected:
00552   size_type _M_node_count; // keeps track of size of tree
00553   _Compare _M_key_compare;
00554 
00555   _Link_type& _M_root() const 
00556     { return (_Link_type&) _M_header->_M_parent; }
00557   _Link_type& _M_leftmost() const 
00558     { return (_Link_type&) _M_header->_M_left; }
00559   _Link_type& _M_rightmost() const 
00560     { return (_Link_type&) _M_header->_M_right; }
00561 
00562   static _Link_type& _S_left(_Link_type __x)
00563     { return (_Link_type&)(__x->_M_left); }
00564   static _Link_type& _S_right(_Link_type __x)
00565     { return (_Link_type&)(__x->_M_right); }
00566   static _Link_type& _S_parent(_Link_type __x)
00567     { return (_Link_type&)(__x->_M_parent); }
00568   static reference _S_value(_Link_type __x)
00569     { return __x->_M_value_field; }
00570   static const _Key& _S_key(_Link_type __x)
00571     { return _KeyOfValue()(_S_value(__x)); }
00572   static _Color_type& _S_color(_Link_type __x)
00573     { return (_Color_type&)(__x->_M_color); }
00574 
00575   static _Link_type& _S_left(_Base_ptr __x)
00576     { return (_Link_type&)(__x->_M_left); }
00577   static _Link_type& _S_right(_Base_ptr __x)
00578     { return (_Link_type&)(__x->_M_right); }
00579   static _Link_type& _S_parent(_Base_ptr __x)
00580     { return (_Link_type&)(__x->_M_parent); }
00581   static reference _S_value(_Base_ptr __x)
00582     { return ((_Link_type)__x)->_M_value_field; }
00583   static const _Key& _S_key(_Base_ptr __x)
00584     { return _KeyOfValue()(_S_value(_Link_type(__x)));} 
00585   static _Color_type& _S_color(_Base_ptr __x)
00586     { return (_Color_type&)(_Link_type(__x)->_M_color); }
00587 
00588   static _Link_type _S_minimum(_Link_type __x) 
00589     { return (_Link_type)  _Rb_tree_node_base::_S_minimum(__x); }
00590 
00591   static _Link_type _S_maximum(_Link_type __x)
00592     { return (_Link_type) _Rb_tree_node_base::_S_maximum(__x); }
00593 
00594 public:
00595   typedef _Rb_tree_iterator<value_type, reference, pointer> iterator;
00596   typedef _Rb_tree_iterator<value_type, const_reference, const_pointer> 
00597           const_iterator;
00598 
00599   typedef reverse_iterator<const_iterator> const_reverse_iterator;
00600   typedef reverse_iterator<iterator> reverse_iterator;
00601 
00602 private:
00603   iterator _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
00604   _Link_type _M_copy(_Link_type __x, _Link_type __p);
00605   void _M_erase(_Link_type __x);
00606 
00607 public:
00608                                 // allocation/deallocation
00609   _Rb_tree()
00610     : _Base(allocator_type()), _M_node_count(0), _M_key_compare()
00611     { _M_empty_initialize(); }
00612 
00613   _Rb_tree(const _Compare& __comp)
00614     : _Base(allocator_type()), _M_node_count(0), _M_key_compare(__comp) 
00615     { _M_empty_initialize(); }
00616 
00617   _Rb_tree(const _Compare& __comp, const allocator_type& __a)
00618     : _Base(__a), _M_node_count(0), _M_key_compare(__comp) 
00619     { _M_empty_initialize(); }
00620 
00621   _Rb_tree(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x) 
00622     : _Base(__x.get_allocator()),
00623       _M_node_count(0), _M_key_compare(__x._M_key_compare)
00624   { 
00625     if (__x._M_root() == 0)
00626       _M_empty_initialize();
00627     else {
00628       _S_color(_M_header) = _S_rb_tree_red;
00629       _M_root() = _M_copy(__x._M_root(), _M_header);
00630       _M_leftmost() = _S_minimum(_M_root());
00631       _M_rightmost() = _S_maximum(_M_root());
00632     }
00633     _M_node_count = __x._M_node_count;
00634   }
00635   ~_Rb_tree() { clear(); }
00636   _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& 
00637   operator=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x);
00638 
00639 private:
00640   void _M_empty_initialize() {
00641     _S_color(_M_header) = _S_rb_tree_red; // used to distinguish header from 
00642                                           // __root, in iterator.operator++
00643     _M_root() = 0;
00644     _M_leftmost() = _M_header;
00645     _M_rightmost() = _M_header;
00646   }
00647 
00648 public:    
00649                                 // accessors:
00650   _Compare key_comp() const { return _M_key_compare; }
00651   iterator begin() { return _M_leftmost(); }
00652   const_iterator begin() const { return _M_leftmost(); }
00653   iterator end() { return _M_header; }
00654   const_iterator end() const { return _M_header; }
00655   reverse_iterator rbegin() { return reverse_iterator(end()); }
00656   const_reverse_iterator rbegin() const { 
00657     return const_reverse_iterator(end()); 
00658   }
00659   reverse_iterator rend() { return reverse_iterator(begin()); }
00660   const_reverse_iterator rend() const { 
00661     return const_reverse_iterator(begin());
00662   } 
00663   bool empty() const { return _M_node_count == 0; }
00664   size_type size() const { return _M_node_count; }
00665   size_type max_size() const { return size_type(-1); }
00666 
00667   void swap(_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __t) {
00668     std::swap(_M_header, __t._M_header);
00669     std::swap(_M_node_count, __t._M_node_count);
00670     std::swap(_M_key_compare, __t._M_key_compare);
00671   }
00672     
00673 public:
00674                                 // insert/erase
00675   pair<iterator,bool> insert_unique(const value_type& __x);
00676   iterator insert_equal(const value_type& __x);
00677 
00678   iterator insert_unique(iterator __position, const value_type& __x);
00679   iterator insert_equal(iterator __position, const value_type& __x);
00680 
00681   template <class _InputIterator>
00682   void insert_unique(_InputIterator __first, _InputIterator __last);
00683   template <class _InputIterator>
00684   void insert_equal(_InputIterator __first, _InputIterator __last);
00685 
00686   void erase(iterator __position);
00687   size_type erase(const key_type& __x);
00688   void erase(iterator __first, iterator __last);
00689   void erase(const key_type* __first, const key_type* __last);
00690   void clear() {
00691     if (_M_node_count != 0) {
00692       _M_erase(_M_root());
00693       _M_leftmost() = _M_header;
00694       _M_root() = 0;
00695       _M_rightmost() = _M_header;
00696       _M_node_count = 0;
00697     }
00698   }      
00699 
00700 public:
00701                                 // set operations:
00702   iterator find(const key_type& __x);
00703   const_iterator find(const key_type& __x) const;
00704   size_type count(const key_type& __x) const;
00705   iterator lower_bound(const key_type& __x);
00706   const_iterator lower_bound(const key_type& __x) const;
00707   iterator upper_bound(const key_type& __x);
00708   const_iterator upper_bound(const key_type& __x) const;
00709   pair<iterator,iterator> equal_range(const key_type& __x);
00710   pair<const_iterator, const_iterator> equal_range(const key_type& __x) const;
00711 
00712 public:
00713                                 // Debugging.
00714   bool __rb_verify() const;
00715 };
00716 
00717 template <class _Key, class _Value, class _KeyOfValue, 
00718           class _Compare, class _Alloc>
00719 inline bool 
00720 operator==(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00721            const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y)
00722 {
00723   return __x.size() == __y.size() &&
00724          equal(__x.begin(), __x.end(), __y.begin());
00725 }
00726 
00727 template <class _Key, class _Value, class _KeyOfValue, 
00728           class _Compare, class _Alloc>
00729 inline bool 
00730 operator<(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00731           const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y)
00732 {
00733   return lexicographical_compare(__x.begin(), __x.end(), 
00734                                  __y.begin(), __y.end());
00735 }
00736 
00737 template <class _Key, class _Value, class _KeyOfValue, 
00738           class _Compare, class _Alloc>
00739 inline bool 
00740 operator!=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00741            const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y) {
00742   return !(__x == __y);
00743 }
00744 
00745 template <class _Key, class _Value, class _KeyOfValue, 
00746           class _Compare, class _Alloc>
00747 inline bool 
00748 operator>(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00749           const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y) {
00750   return __y < __x;
00751 }
00752 
00753 template <class _Key, class _Value, class _KeyOfValue, 
00754           class _Compare, class _Alloc>
00755 inline bool 
00756 operator<=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00757            const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y) {
00758   return !(__y < __x);
00759 }
00760 
00761 template <class _Key, class _Value, class _KeyOfValue, 
00762           class _Compare, class _Alloc>
00763 inline bool 
00764 operator>=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00765            const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y) {
00766   return !(__x < __y);
00767 }
00768 
00769 
00770 template <class _Key, class _Value, class _KeyOfValue, 
00771           class _Compare, class _Alloc>
00772 inline void 
00773 swap(_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x, 
00774      _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __y)
00775 {
00776   __x.swap(__y);
00777 }
00778 
00779 
00780 template <class _Key, class _Value, class _KeyOfValue, 
00781           class _Compare, class _Alloc>
00782 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& 
00783 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
00784   ::operator=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x)
00785 {
00786   if (this != &__x) {
00787                                 // Note that _Key may be a constant type.
00788     clear();
00789     _M_node_count = 0;
00790     _M_key_compare = __x._M_key_compare;        
00791     if (__x._M_root() == 0) {
00792       _M_root() = 0;
00793       _M_leftmost() = _M_header;
00794       _M_rightmost() = _M_header;
00795     }
00796     else {
00797       _M_root() = _M_copy(__x._M_root(), _M_header);
00798       _M_leftmost() = _S_minimum(_M_root());
00799       _M_rightmost() = _S_maximum(_M_root());
00800       _M_node_count = __x._M_node_count;
00801     }
00802   }
00803   return *this;
00804 }
00805 
00806 template <class _Key, class _Value, class _KeyOfValue, 
00807           class _Compare, class _Alloc>
00808 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator
00809 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
00810   ::_M_insert(_Base_ptr __x_, _Base_ptr __y_, const _Value& __v)
00811 {
00812   _Link_type __x = (_Link_type) __x_;
00813   _Link_type __y = (_Link_type) __y_;
00814   _Link_type __z;
00815 
00816   if (__y == _M_header || __x != 0 || 
00817       _M_key_compare(_KeyOfValue()(__v), _S_key(__y))) {
00818     __z = _M_create_node(__v);
00819     _S_left(__y) = __z;               // also makes _M_leftmost() = __z 
00820                                       //    when __y == _M_header
00821     if (__y == _M_header) {
00822       _M_root() = __z;
00823       _M_rightmost() = __z;
00824     }
00825     else if (__y == _M_leftmost())
00826       _M_leftmost() = __z;   // maintain _M_leftmost() pointing to min node
00827   }
00828   else {
00829     __z = _M_create_node(__v);
00830     _S_right(__y) = __z;
00831     if (__y == _M_rightmost())
00832       _M_rightmost() = __z;  // maintain _M_rightmost() pointing to max node
00833   }
00834   _S_parent(__z) = __y;
00835   _S_left(__z) = 0;
00836   _S_right(__z) = 0;
00837   _Rb_tree_rebalance(__z, _M_header->_M_parent);
00838   ++_M_node_count;
00839   return iterator(__z);
00840 }
00841 
00842 template <class _Key, class _Value, class _KeyOfValue, 
00843           class _Compare, class _Alloc>
00844 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator
00845 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
00846   ::insert_equal(const _Value& __v)
00847 {
00848   _Link_type __y = _M_header;
00849   _Link_type __x = _M_root();
00850   while (__x != 0) {
00851     __y = __x;
00852     __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ? 
00853             _S_left(__x) : _S_right(__x);
00854   }
00855   return _M_insert(__x, __y, __v);
00856 }
00857 
00858 
00859 template <class _Key, class _Value, class _KeyOfValue, 
00860           class _Compare, class _Alloc>
00861 pair<typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator, 
00862      bool>
00863 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
00864   ::insert_unique(const _Value& __v)
00865 {
00866   _Link_type __y = _M_header;
00867   _Link_type __x = _M_root();
00868   bool __comp = true;
00869   while (__x != 0) {
00870     __y = __x;
00871     __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
00872     __x = __comp ? _S_left(__x) : _S_right(__x);
00873   }
00874   iterator __j = iterator(__y);   
00875   if (__comp)
00876     if (__j == begin())     
00877       return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
00878     else
00879       --__j;
00880   if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
00881     return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
00882   return pair<iterator,bool>(__j, false);
00883 }
00884 
00885 
00886 template <class _Key, class _Val, class _KeyOfValue, 
00887           class _Compare, class _Alloc>
00888 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator 
00889 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>
00890   ::insert_unique(iterator __position, const _Val& __v)
00891 {
00892   if (__position._M_node == _M_header->_M_left) { // begin()
00893     if (size() > 0 && 
00894        _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))
00895       return _M_insert(__position._M_node, __position._M_node, __v);
00896     // first argument just needs to be non-null 
00897     else
00898       return insert_unique(__v).first;
00899   } else if (__position._M_node == _M_header) { // end()
00900     if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
00901       return _M_insert(0, _M_rightmost(), __v);
00902     else
00903       return insert_unique(__v).first;
00904   } else {
00905     iterator __before = __position;
00906     --__before;
00907     if (_M_key_compare(_S_key(__before._M_node), _KeyOfValue()(__v)) 
00908         && _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node))) {
00909       if (_S_right(__before._M_node) == 0)
00910         return _M_insert(0, __before._M_node, __v); 
00911       else
00912         return _M_insert(__position._M_node, __position._M_node, __v);
00913     // first argument just needs to be non-null 
00914     } else
00915       return insert_unique(__v).first;
00916   }
00917 }
00918 
00919 template <class _Key, class _Val, class _KeyOfValue, 
00920           class _Compare, class _Alloc>
00921 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator 
00922 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>
00923   ::insert_equal(iterator __position, const _Val& __v)
00924 {
00925   if (__position._M_node == _M_header->_M_left) { // begin()
00926     if (size() > 0 && 
00927     !_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))
00928       return _M_insert(__position._M_node, __position._M_node, __v);
00929     // first argument just needs to be non-null 
00930     else
00931       return insert_equal(__v);
00932   } else if (__position._M_node == _M_header) {// end()
00933     if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
00934       return _M_insert(0, _M_rightmost(), __v);
00935     else
00936       return insert_equal(__v);
00937   } else {
00938     iterator __before = __position;
00939     --__before;
00940     if (!_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))
00941         && !_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v))) {
00942       if (_S_right(__before._M_node) == 0)
00943         return _M_insert(0, __before._M_node, __v); 
00944       else
00945         return _M_insert(__position._M_node, __position._M_node, __v);
00946     // first argument just needs to be non-null 
00947     } else
00948       return insert_equal(__v);
00949   }
00950 }
00951 
00952 template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc>
00953   template<class _II>
00954 void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>
00955   ::insert_equal(_II __first, _II __last)
00956 {
00957   for ( ; __first != __last; ++__first)
00958     insert_equal(*__first);
00959 }
00960 
00961 template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc> 
00962   template<class _II>
00963 void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>
00964   ::insert_unique(_II __first, _II __last) {
00965   for ( ; __first != __last; ++__first)
00966     insert_unique(*__first);
00967 }
00968 
00969 template <class _Key, class _Value, class _KeyOfValue, 
00970           class _Compare, class _Alloc>
00971 inline void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
00972   ::erase(iterator __position)
00973 {
00974   _Link_type __y = 
00975     (_Link_type) _Rb_tree_rebalance_for_erase(__position._M_node,
00976                                               _M_header->_M_parent,
00977                                               _M_header->_M_left,
00978                                               _M_header->_M_right);
00979   destroy_node(__y);
00980   --_M_node_count;
00981 }
00982 
00983 template <class _Key, class _Value, class _KeyOfValue, 
00984           class _Compare, class _Alloc>
00985 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::size_type 
00986 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::erase(const _Key& __x)
00987 {
00988   pair<iterator,iterator> __p = equal_range(__x);
00989   size_type __n = 0;
00990   distance(__p.first, __p.second, __n);
00991   erase(__p.first, __p.second);
00992   return __n;
00993 }
00994 
00995 template <class _Key, class _Val, class _KoV, class _Compare, class _Alloc>
00996 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type 
00997 _Rb_tree<_Key,_Val,_KoV,_Compare,_Alloc>
00998   ::_M_copy(_Link_type __x, _Link_type __p)
00999 {
01000                         // structural copy.  __x and __p must be non-null.
01001   _Link_type __top = _M_clone_node(__x);
01002   __top->_M_parent = __p;
01003  
01004   __STL_TRY {
01005     if (__x->_M_right)
01006       __top->_M_right = _M_copy(_S_right(__x), __top);
01007     __p = __top;
01008     __x = _S_left(__x);
01009 
01010     while (__x != 0) {
01011       _Link_type __y = _M_clone_node(__x);
01012       __p->_M_left = __y;
01013       __y->_M_parent = __p;
01014       if (__x->_M_right)
01015         __y->_M_right = _M_copy(_S_right(__x), __y);
01016       __p = __y;
01017       __x = _S_left(__x);
01018     }
01019   }
01020   __STL_UNWIND(_M_erase(__top));
01021 
01022   return __top;
01023 }
01024 
01025 template <class _Key, class _Value, class _KeyOfValue, 
01026           class _Compare, class _Alloc>
01027 void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01028   ::_M_erase(_Link_type __x)
01029 {
01030                                 // erase without rebalancing
01031   while (__x != 0) {
01032     _M_erase(_S_right(__x));
01033     _Link_type __y = _S_left(__x);
01034     destroy_node(__x);
01035     __x = __y;
01036   }
01037 }
01038 
01039 template <class _Key, class _Value, class _KeyOfValue, 
01040           class _Compare, class _Alloc>
01041 void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01042   ::erase(iterator __first, iterator __last)
01043 {
01044   if (__first == begin() && __last == end())
01045     clear();
01046   else
01047     while (__first != __last) erase(__first++);
01048 }
01049 
01050 template <class _Key, class _Value, class _KeyOfValue, 
01051           class _Compare, class _Alloc>
01052 void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01053   ::erase(const _Key* __first, const _Key* __last) 
01054 {
01055   while (__first != __last) erase(*__first++);
01056 }
01057 
01058 template <class _Key, class _Value, class _KeyOfValue, 
01059           class _Compare, class _Alloc>
01060 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator 
01061 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k)
01062 {
01063   _Link_type __y = _M_header;      // Last node which is not less than __k. 
01064   _Link_type __x = _M_root();      // Current node. 
01065 
01066   while (__x != 0) 
01067     if (!_M_key_compare(_S_key(__x), __k))
01068       __y = __x, __x = _S_left(__x);
01069     else
01070       __x = _S_right(__x);
01071 
01072   iterator __j = iterator(__y);   
01073   return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ? 
01074      end() : __j;
01075 }
01076 
01077 template <class _Key, class _Value, class _KeyOfValue, 
01078           class _Compare, class _Alloc>
01079 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator 
01080 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k) const
01081 {
01082   _Link_type __y = _M_header; /* Last node which is not less than __k. */
01083   _Link_type __x = _M_root(); /* Current node. */
01084 
01085   while (__x != 0) {
01086     if (!_M_key_compare(_S_key(__x), __k))
01087       __y = __x, __x = _S_left(__x);
01088     else
01089       __x = _S_right(__x);
01090   }
01091   const_iterator __j = const_iterator(__y);   
01092   return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
01093     end() : __j;
01094 }
01095 
01096 template <class _Key, class _Value, class _KeyOfValue, 
01097           class _Compare, class _Alloc>
01098 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::size_type 
01099 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01100   ::count(const _Key& __k) const
01101 {
01102   pair<const_iterator, const_iterator> __p = equal_range(__k);
01103   size_type __n = 0;
01104   distance(__p.first, __p.second, __n);
01105   return __n;
01106 }
01107 
01108 template <class _Key, class _Value, class _KeyOfValue, 
01109           class _Compare, class _Alloc>
01110 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator 
01111 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01112   ::lower_bound(const _Key& __k)
01113 {
01114   _Link_type __y = _M_header; /* Last node which is not less than __k. */
01115   _Link_type __x = _M_root(); /* Current node. */
01116 
01117   while (__x != 0) 
01118     if (!_M_key_compare(_S_key(__x), __k))
01119       __y = __x, __x = _S_left(__x);
01120     else
01121       __x = _S_right(__x);
01122 
01123   return iterator(__y);
01124 }
01125 
01126 template <class _Key, class _Value, class _KeyOfValue, 
01127           class _Compare, class _Alloc>
01128 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator 
01129 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01130   ::lower_bound(const _Key& __k) const
01131 {
01132   _Link_type __y = _M_header; /* Last node which is not less than __k. */
01133   _Link_type __x = _M_root(); /* Current node. */
01134 
01135   while (__x != 0) 
01136     if (!_M_key_compare(_S_key(__x), __k))
01137       __y = __x, __x = _S_left(__x);
01138     else
01139       __x = _S_right(__x);
01140 
01141   return const_iterator(__y);
01142 }
01143 
01144 template <class _Key, class _Value, class _KeyOfValue, 
01145           class _Compare, class _Alloc>
01146 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator 
01147 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01148   ::upper_bound(const _Key& __k)
01149 {
01150   _Link_type __y = _M_header; /* Last node which is greater than __k. */
01151   _Link_type __x = _M_root(); /* Current node. */
01152 
01153    while (__x != 0) 
01154      if (_M_key_compare(__k, _S_key(__x)))
01155        __y = __x, __x = _S_left(__x);
01156      else
01157        __x = _S_right(__x);
01158 
01159    return iterator(__y);
01160 }
01161 
01162 template <class _Key, class _Value, class _KeyOfValue, 
01163           class _Compare, class _Alloc>
01164 typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator 
01165 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01166   ::upper_bound(const _Key& __k) const
01167 {
01168   _Link_type __y = _M_header; /* Last node which is greater than __k. */
01169   _Link_type __x = _M_root(); /* Current node. */
01170 
01171    while (__x != 0) 
01172      if (_M_key_compare(__k, _S_key(__x)))
01173        __y = __x, __x = _S_left(__x);
01174      else
01175        __x = _S_right(__x);
01176 
01177    return const_iterator(__y);
01178 }
01179 
01180 template <class _Key, class _Value, class _KeyOfValue, 
01181           class _Compare, class _Alloc>
01182 inline 
01183 pair<typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator,
01184      typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator>
01185 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
01186   ::equal_range(const _Key& __k)
01187 {
01188   return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k));
01189 }
01190 
01191 template <class _Key, class _Value, class _KoV, class _Compare, class _Alloc>
01192 inline 
01193 pair<typename _Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc>::const_iterator,
01194      typename _Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc>::const_iterator>
01195 _Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc>
01196   ::equal_range(const _Key& __k) const
01197 {
01198   return pair<const_iterator,const_iterator>(lower_bound(__k),
01199                                              upper_bound(__k));
01200 }
01201 
01202 inline int
01203 __black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root)
01204 {
01205   if (__node == 0)
01206     return 0;
01207   int __sum = 0;
01208   do {
01209     if (__node->_M_color == _S_rb_tree_black) 
01210       ++__sum;
01211     if (__node == __root) 
01212       break;
01213     __node = __node->_M_parent;
01214   } while (1);
01215   return __sum;
01216 }
01217 
01218 template <class _Key, class _Value, class _KeyOfValue, 
01219           class _Compare, class _Alloc>
01220 bool _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
01221 {
01222   if (_M_node_count == 0 || begin() == end())
01223     return _M_node_count == 0 && begin() == end() &&
01224       _M_header->_M_left == _M_header && _M_header->_M_right == _M_header;
01225   
01226   int __len = __black_count(_M_leftmost(), _M_root());
01227   for (const_iterator __it = begin(); __it != end(); ++__it) {
01228     _Link_type __x = (_Link_type) __it._M_node;
01229     _Link_type __L = _S_left(__x);
01230     _Link_type __R = _S_right(__x);
01231 
01232     if (__x->_M_color == _S_rb_tree_red)
01233       if ((__L && __L->_M_color == _S_rb_tree_red) ||
01234           (__R && __R->_M_color == _S_rb_tree_red))
01235         return false;
01236 
01237     if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
01238       return false;
01239     if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
01240       return false;
01241 
01242     if (!__L && !__R && __black_count(__x, _M_root()) != __len)
01243       return false;
01244   }
01245 
01246   if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
01247     return false;
01248   if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
01249     return false;
01250 
01251   return true;
01252 }
01253 
01254 // Class rb_tree is not part of the C++ standard.  It is provided for
01255 // compatibility with the HP STL.
01256 
01257 template <class _Key, class _Value, class _KeyOfValue, class _Compare,
01258           class _Alloc = allocator<_Value> >
01259 struct rb_tree : public _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>
01260 {
01261   typedef _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc> _Base;
01262   typedef typename _Base::allocator_type allocator_type;
01263 
01264   rb_tree(const _Compare& __comp = _Compare(),
01265           const allocator_type& __a = allocator_type())
01266     : _Base(__comp, __a) {}
01267   
01268   ~rb_tree() {}
01269 };
01270 
01271 } // namespace std 
01272 
01273 #endif /* __SGI_STL_INTERNAL_TREE_H */
01274 
01275 // Local Variables:
01276 // mode:C++
01277 // End:

Generated at Tue May 1 16:28:40 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001