stl_tree.h

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

Generated on Mon Jun 5 10:20:45 2006 for Intelligence.kdevelop by  doxygen 1.4.6