epoc32/include/stdapis/stlport/stl/_tree.c
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     1 /*
     2  *
     3  *
     4  * Copyright (c) 1994
     5  * Hewlett-Packard Company
     6  *
     7  * Copyright (c) 1996,1997
     8  * Silicon Graphics Computer Systems, Inc.
     9  *
    10  * Copyright (c) 1997
    11  * Moscow Center for SPARC Technology
    12  *
    13  * Copyright (c) 1999 
    14  * Boris Fomitchev
    15  *
    16  * This material is provided "as is", with absolutely no warranty expressed
    17  * or implied. Any use is at your own risk.
    18  *
    19  * Permission to use or copy this software for any purpose is hereby granted 
    20  * without fee, provided the above notices are retained on all copies.
    21  * Permission to modify the code and to distribute modified code is granted,
    22  * provided the above notices are retained, and a notice that the code was
    23  * modified is included with the above copyright notice.
    24  *
    25  * Modified CRP 7/10/00 for improved conformance / efficiency on insert_unique /
    26  * insert_equal with valid hint -- efficiency is improved all around, and it is
    27  * should now be standard conforming for complexity on insert point immediately
    28  * after hint (amortized constant time).
    29  *
    30  */
    31 #ifndef _STLP_TREE_C
    32 #define _STLP_TREE_C
    33 
    34 #ifndef _STLP_INTERNAL_TREE_H
    35 # include <stl/_tree.h>
    36 #endif
    37 
    38 // fbp: these defines are for outline methods definitions.
    39 // needed for definitions to be portable. Should not be used in method bodies.
    40 # if defined  ( _STLP_NESTED_TYPE_PARAM_BUG )
    41 #  define __iterator__        _Rb_tree_iterator<_Value, _Nonconst_traits<_Value> > 
    42 #  define __size_type__       size_t
    43 #  define iterator __iterator__
    44 # else
    45 #  define __iterator__  _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>::iterator
    46 #  define __size_type__  _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>::size_type
    47 # endif
    48 
    49 #if defined ( _STLP_DEBUG)
    50 #  define _Rb_tree __WORKAROUND_DBG_RENAME(Rb_tree)
    51 #endif
    52 
    53 _STLP_BEGIN_NAMESPACE
    54 
    55 # if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
    56 
    57 template <class _Dummy> void _STLP_CALL
    58 _Rb_global<_Dummy>::_Rotate_left(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
    59 {
    60   _Rb_tree_node_base* __y = __x->_M_right;
    61   __x->_M_right = __y->_M_left;
    62   if (__y->_M_left !=0)
    63     __y->_M_left->_M_parent = __x;
    64   __y->_M_parent = __x->_M_parent;
    65 
    66   if (__x == __root)
    67     __root = __y;
    68   else if (__x == __x->_M_parent->_M_left)
    69     __x->_M_parent->_M_left = __y;
    70   else
    71     __x->_M_parent->_M_right = __y;
    72   __y->_M_left = __x;
    73   __x->_M_parent = __y;
    74 }
    75 
    76 template <class _Dummy> void _STLP_CALL 
    77 _Rb_global<_Dummy>::_Rotate_right(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
    78 {
    79   _Rb_tree_node_base* __y = __x->_M_left;
    80   __x->_M_left = __y->_M_right;
    81   if (__y->_M_right != 0)
    82     __y->_M_right->_M_parent = __x;
    83   __y->_M_parent = __x->_M_parent;
    84 
    85   if (__x == __root)
    86     __root = __y;
    87   else if (__x == __x->_M_parent->_M_right)
    88     __x->_M_parent->_M_right = __y;
    89   else
    90     __x->_M_parent->_M_left = __y;
    91   __y->_M_right = __x;
    92   __x->_M_parent = __y;
    93 }
    94 
    95 template <class _Dummy> void _STLP_CALL
    96 _Rb_global<_Dummy>::_Rebalance(_Rb_tree_node_base* __x, 
    97 			       _Rb_tree_node_base*& __root)
    98 {
    99   __x->_M_color = _S_rb_tree_red;
   100   while (__x != __root && __x->_M_parent->_M_color == _S_rb_tree_red) {
   101     if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) {
   102       _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
   103       if (__y && __y->_M_color == _S_rb_tree_red) {
   104         __x->_M_parent->_M_color = _S_rb_tree_black;
   105         __y->_M_color = _S_rb_tree_black;
   106         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
   107         __x = __x->_M_parent->_M_parent;
   108       }
   109       else {
   110         if (__x == __x->_M_parent->_M_right) {
   111           __x = __x->_M_parent;
   112           _Rotate_left(__x, __root);
   113         }
   114         __x->_M_parent->_M_color = _S_rb_tree_black;
   115         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
   116         _Rotate_right(__x->_M_parent->_M_parent, __root);
   117       }
   118     }
   119     else {
   120       _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
   121       if (__y && __y->_M_color == _S_rb_tree_red) {
   122         __x->_M_parent->_M_color = _S_rb_tree_black;
   123         __y->_M_color = _S_rb_tree_black;
   124         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
   125         __x = __x->_M_parent->_M_parent;
   126       }
   127       else {
   128         if (__x == __x->_M_parent->_M_left) {
   129           __x = __x->_M_parent;
   130           _Rotate_right(__x, __root);
   131         }
   132         __x->_M_parent->_M_color = _S_rb_tree_black;
   133         __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
   134         _Rotate_left(__x->_M_parent->_M_parent, __root);
   135       }
   136     }
   137   }
   138   __root->_M_color = _S_rb_tree_black;
   139 }
   140 
   141 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
   142 _Rb_global<_Dummy>::_Rebalance_for_erase(_Rb_tree_node_base* __z,
   143 					 _Rb_tree_node_base*& __root,
   144 					 _Rb_tree_node_base*& __leftmost,
   145 					 _Rb_tree_node_base*& __rightmost)
   146 {
   147   _Rb_tree_node_base* __y = __z;
   148   _Rb_tree_node_base* __x = 0;
   149   _Rb_tree_node_base* __x_parent = 0;
   150   if (__y->_M_left == 0)     // __z has at most one non-null child. y == z.
   151     __x = __y->_M_right;     // __x might be null.
   152   else
   153     if (__y->_M_right == 0)  // __z has exactly one non-null child. y == z.
   154       __x = __y->_M_left;    // __x is not null.
   155     else {                   // __z has two non-null children.  Set __y to
   156       __y = __y->_M_right;   //   __z's successor.  __x might be null.
   157       while (__y->_M_left != 0)
   158         __y = __y->_M_left;
   159       __x = __y->_M_right;
   160     }
   161   if (__y != __z) {          // relink y in place of z.  y is z's successor
   162     __z->_M_left->_M_parent = __y; 
   163     __y->_M_left = __z->_M_left;
   164     if (__y != __z->_M_right) {
   165       __x_parent = __y->_M_parent;
   166       if (__x) __x->_M_parent = __y->_M_parent;
   167       __y->_M_parent->_M_left = __x;      // __y must be a child of _M_left
   168       __y->_M_right = __z->_M_right;
   169       __z->_M_right->_M_parent = __y;
   170     }
   171     else
   172       __x_parent = __y;  
   173     if (__root == __z)
   174       __root = __y;
   175     else if (__z->_M_parent->_M_left == __z)
   176       __z->_M_parent->_M_left = __y;
   177     else 
   178       __z->_M_parent->_M_right = __y;
   179     __y->_M_parent = __z->_M_parent;
   180     _STLP_STD::swap(__y->_M_color, __z->_M_color);
   181     __y = __z;
   182     // __y now points to node to be actually deleted
   183   }
   184   else {                        // __y == __z
   185     __x_parent = __y->_M_parent;
   186     if (__x) __x->_M_parent = __y->_M_parent;   
   187     if (__root == __z)
   188       __root = __x;
   189     else 
   190       if (__z->_M_parent->_M_left == __z)
   191         __z->_M_parent->_M_left = __x;
   192       else
   193         __z->_M_parent->_M_right = __x;
   194     if (__leftmost == __z) 
   195       if (__z->_M_right == 0)        // __z->_M_left must be null also
   196         __leftmost = __z->_M_parent;
   197     // makes __leftmost == _M_header if __z == __root
   198       else
   199         __leftmost = _Rb_tree_node_base::_S_minimum(__x);
   200     if (__rightmost == __z)  
   201       if (__z->_M_left == 0)         // __z->_M_right must be null also
   202         __rightmost = __z->_M_parent;  
   203     // makes __rightmost == _M_header if __z == __root
   204       else                      // __x == __z->_M_left
   205         __rightmost = _Rb_tree_node_base::_S_maximum(__x);
   206   }
   207   if (__y->_M_color != _S_rb_tree_red) { 
   208     while (__x != __root && (__x == 0 || __x->_M_color == _S_rb_tree_black))
   209       if (__x == __x_parent->_M_left) {
   210         _Rb_tree_node_base* __w = __x_parent->_M_right;
   211         if (__w->_M_color == _S_rb_tree_red) {
   212           __w->_M_color = _S_rb_tree_black;
   213           __x_parent->_M_color = _S_rb_tree_red;
   214           _Rotate_left(__x_parent, __root);
   215           __w = __x_parent->_M_right;
   216         }
   217         if ((__w->_M_left == 0 || 
   218              __w->_M_left->_M_color == _S_rb_tree_black) && (__w->_M_right == 0 || 
   219              __w->_M_right->_M_color == _S_rb_tree_black)) {
   220           __w->_M_color = _S_rb_tree_red;
   221           __x = __x_parent;
   222           __x_parent = __x_parent->_M_parent;
   223         } else {
   224           if (__w->_M_right == 0 || 
   225               __w->_M_right->_M_color == _S_rb_tree_black) {
   226             if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
   227             __w->_M_color = _S_rb_tree_red;
   228             _Rotate_right(__w, __root);
   229             __w = __x_parent->_M_right;
   230           }
   231           __w->_M_color = __x_parent->_M_color;
   232           __x_parent->_M_color = _S_rb_tree_black;
   233           if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
   234           _Rotate_left(__x_parent, __root);
   235           break;
   236         }
   237       } else {                  // same as above, with _M_right <-> _M_left.
   238         _Rb_tree_node_base* __w = __x_parent->_M_left;
   239         if (__w->_M_color == _S_rb_tree_red) {
   240           __w->_M_color = _S_rb_tree_black;
   241           __x_parent->_M_color = _S_rb_tree_red;
   242           _Rotate_right(__x_parent, __root);
   243           __w = __x_parent->_M_left;
   244         }
   245         if ((__w->_M_right == 0 || 
   246              __w->_M_right->_M_color == _S_rb_tree_black) && (__w->_M_left == 0 || 
   247              __w->_M_left->_M_color == _S_rb_tree_black)) {
   248           __w->_M_color = _S_rb_tree_red;
   249           __x = __x_parent;
   250           __x_parent = __x_parent->_M_parent;
   251         } else {
   252           if (__w->_M_left == 0 || 
   253               __w->_M_left->_M_color == _S_rb_tree_black) {
   254             if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
   255             __w->_M_color = _S_rb_tree_red;
   256             _Rotate_left(__w, __root);
   257             __w = __x_parent->_M_left;
   258           }
   259           __w->_M_color = __x_parent->_M_color;
   260           __x_parent->_M_color = _S_rb_tree_black;
   261           if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
   262           _Rotate_right(__x_parent, __root);
   263           break;
   264         }
   265       }
   266     if (__x) __x->_M_color = _S_rb_tree_black;
   267   }
   268   return __y;
   269 }
   270 
   271 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
   272 _Rb_global<_Dummy>::_M_decrement(_Rb_tree_node_base* _M_node)
   273 {
   274   if (_M_node->_M_color == _S_rb_tree_red && _M_node->_M_parent->_M_parent == _M_node)
   275     _M_node = _M_node->_M_right;
   276   else if (_M_node->_M_left != 0) {
   277     _Base_ptr __y = _M_node->_M_left;
   278     while (__y->_M_right != 0)
   279       __y = __y->_M_right;
   280     _M_node = __y;
   281   }
   282   else {
   283     _Base_ptr __y = _M_node->_M_parent;
   284     while (_M_node == __y->_M_left) {
   285       _M_node = __y;
   286       __y = __y->_M_parent;
   287     }
   288     _M_node = __y;
   289   }
   290   return _M_node;
   291 }
   292 
   293 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
   294 _Rb_global<_Dummy>::_M_increment(_Rb_tree_node_base* _M_node)
   295 {
   296   if (_M_node->_M_right != 0) {
   297     _M_node = _M_node->_M_right;
   298     while (_M_node->_M_left != 0)
   299       _M_node = _M_node->_M_left;
   300   }
   301   else {
   302     _Base_ptr __y = _M_node->_M_parent;
   303     while (_M_node == __y->_M_right) {
   304       _M_node = __y;
   305       __y = __y->_M_parent;
   306     }
   307     if (_M_node->_M_right != __y)
   308       _M_node = __y;
   309   }
   310   return _M_node;
   311 }
   312 
   313 #endif /* defined (__BUILDING_STLPORT) || ! defined (_STLP_OWN_IOSTREAMS) */
   314 
   315 
   316 template <class _Key, class _Value, class _KeyOfValue, 
   317           class _Compare, class _Alloc> _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::operator=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x)
   318 {
   319   if (this != &__x) {
   320                                 // Note that _Key may be a constant type.
   321     clear();
   322     _M_node_count = 0;
   323     _M_key_compare = __x._M_key_compare;        
   324     if (__x._M_root() == 0) {
   325       _M_root() = 0;
   326       _M_leftmost() = this->_M_header._M_data;
   327       _M_rightmost() = this->_M_header._M_data;
   328     }
   329     else {
   330       _M_root() = _M_copy(__x._M_root(), this->_M_header._M_data);
   331       _M_leftmost() = _S_minimum(_M_root());
   332       _M_rightmost() = _S_maximum(_M_root());
   333       _M_node_count = __x._M_node_count;
   334     }
   335   }
   336   return *this;
   337 }
   338 
   339 // CRP 7/10/00 inserted argument __w_, which is another hint (meant to
   340 // act like __x_ and ignore a portion of the if conditions -- specify
   341 // __w_ != 0 to bypass comparison as false or __x_ != 0 to bypass
   342 // comparison as true)
   343 template <class _Key, class _Value, class _KeyOfValue, 
   344           class _Compare, class _Alloc> __iterator__ 
   345 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::_M_insert(_Rb_tree_node_base* __x_, _Rb_tree_node_base* __y_, const _Value& __v,
   346   _Rb_tree_node_base* __w_)
   347 {
   348   _Link_type __w = (_Link_type) __w_;
   349   _Link_type __x = (_Link_type) __x_;
   350   _Link_type __y = (_Link_type) __y_;
   351   _Link_type __z;
   352 
   353   if ( __y == this->_M_header._M_data ||
   354        ( __w == 0 && // If w != 0, the remainder fails to false
   355          ( __x != 0 ||     // If x != 0, the remainder succeeds to true
   356            _M_key_compare( _KeyOfValue()(__v), _S_key(__y) ) )
   357 	 )
   358        ) {
   359     
   360     __z = _M_create_node(__v);
   361     _S_left(__y) = __z;               // also makes _M_leftmost() = __z 
   362                                       //    when __y == _M_header
   363     if (__y == this->_M_header._M_data) {
   364       _M_root() = __z;
   365       _M_rightmost() = __z;
   366     }
   367     else if (__y == _M_leftmost())
   368       _M_leftmost() = __z;   // maintain _M_leftmost() pointing to min node
   369   }
   370   else {
   371     __z = _M_create_node(__v);
   372     _S_right(__y) = __z;
   373     if (__y == _M_rightmost())
   374       _M_rightmost() = __z;  // maintain _M_rightmost() pointing to max node
   375   }
   376   _S_parent(__z) = __y;
   377   _S_left(__z) = 0;
   378   _S_right(__z) = 0;
   379   _Rb_global_inst::_Rebalance(__z, this->_M_header._M_data->_M_parent);
   380   ++_M_node_count;
   381   return iterator(__z);
   382 }
   383 
   384 template <class _Key, class _Value, class _KeyOfValue, 
   385           class _Compare, class _Alloc> __iterator__
   386 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_equal(const _Value& __v)
   387 {
   388   _Link_type __y = this->_M_header._M_data;
   389   _Link_type __x = _M_root();
   390   while (__x != 0) {
   391     __y = __x;
   392     __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ? 
   393             _S_left(__x) : _S_right(__x);
   394   }
   395   return _M_insert(__x, __y, __v);
   396 }
   397 
   398 
   399 template <class _Key, class _Value, class _KeyOfValue, 
   400           class _Compare, class _Alloc> pair< _Rb_tree_iterator<_Value, _Nonconst_traits<_Value> >, bool> _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_unique(const _Value& __v)
   401 {
   402   _Link_type __y = this->_M_header._M_data;
   403   _Link_type __x = _M_root();
   404   bool __comp = true;
   405   while (__x != 0) {
   406     __y = __x;
   407     __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
   408     __x = __comp ? _S_left(__x) : _S_right(__x);
   409   }
   410   iterator __j = iterator(__y);   
   411   if (__comp)
   412     if (__j == begin())     
   413       return pair<iterator,bool>(_M_insert(/* __x*/ __y, __y, __v), true);
   414     else
   415       --__j;
   416   if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
   417     return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
   418   return pair<iterator,bool>(__j, false);
   419 }
   420 
   421 // Modifications CRP 7/10/00 as noted to improve conformance and
   422 // efficiency.
   423 template <class _Key, class _Value, class _KeyOfValue, 
   424           class _Compare, class _Alloc> __iterator__ 
   425 _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc> ::insert_unique(iterator __position, const _Value& __v)
   426 {
   427   if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()
   428 
   429     // if the container is empty, fall back on insert_unique.
   430     if (size() <= 0)
   431       return insert_unique(__v).first;
   432 
   433     if ( _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
   434       return _M_insert(__position._M_node, __position._M_node, __v);
   435     // first argument just needs to be non-null 
   436     else
   437       {
   438 	bool __comp_pos_v = _M_key_compare( _S_key(__position._M_node), _KeyOfValue()(__v) );
   439 	
   440 	if (__comp_pos_v == false)  // compare > and compare < both false so compare equal
   441 	  return __position;
   442 	//Below __comp_pos_v == true
   443 
   444 	// Standard-conformance - does the insertion point fall immediately AFTER
   445 	// the hint?
   446 	iterator __after = __position;
   447 	++__after;
   448 
   449 	// Check for only one member -- in that case, __position points to itself,
   450 	// and attempting to increment will cause an infinite loop.
   451 	if (__after._M_node == this->_M_header._M_data)
   452 	  // Check guarantees exactly one member, so comparison was already
   453 	  // performed and we know the result; skip repeating it in _M_insert
   454 	  // by specifying a non-zero fourth argument.
   455 	  return _M_insert(0, __position._M_node, __v, __position._M_node);
   456 		
   457 	
   458 	// All other cases:
   459 	
   460 	// Optimization to catch insert-equivalent -- save comparison results,
   461 	// and we get this for free.
   462 	if(_M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) )) {
   463 	  if (_S_right(__position._M_node) == 0)
   464 	    return _M_insert(0, __position._M_node, __v, __position._M_node);
   465 	  else
   466 	    return _M_insert(__after._M_node, __after._M_node, __v);
   467 	} else {
   468 	    return insert_unique(__v).first;
   469 	}
   470       }
   471 
   472   } else if (__position._M_node == this->_M_header._M_data) { // end()
   473     if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
   474       // pass along to _M_insert that it can skip comparing
   475       // v, Key ; since compare Key, v was true, compare v, Key must be false.
   476       return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null
   477     else
   478       return insert_unique(__v).first;
   479   } else {
   480     iterator __before = __position;
   481     --__before;
   482     
   483     bool __comp_v_pos = _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node));
   484 
   485     if (__comp_v_pos
   486       && _M_key_compare( _S_key(__before._M_node), _KeyOfValue()(__v) )) {
   487 
   488       if (_S_right(__before._M_node) == 0)
   489         return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null
   490       else
   491         return _M_insert(__position._M_node, __position._M_node, __v);
   492     // first argument just needs to be non-null 
   493     } else
   494       {
   495 	// Does the insertion point fall immediately AFTER the hint?
   496 	iterator __after = __position;
   497 	++__after;
   498 	
   499 	// Optimization to catch equivalent cases and avoid unnecessary comparisons
   500 	bool __comp_pos_v = !__comp_v_pos;  // Stored this result earlier
   501 	// If the earlier comparison was true, this comparison doesn't need to be
   502 	// performed because it must be false.  However, if the earlier comparison
   503 	// was false, we need to perform this one because in the equal case, both will
   504 	// be false.
   505 	if (!__comp_v_pos) __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));
   506 	
   507 	if ( (!__comp_v_pos) // comp_v_pos true implies comp_v_pos false
   508 	     && __comp_pos_v
   509 	     && (__after._M_node == this->_M_header._M_data ||
   510 	        _M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) ))) {
   511 	  
   512 	  if (_S_right(__position._M_node) == 0)
   513 	    return _M_insert(0, __position._M_node, __v, __position._M_node);
   514 	  else
   515 	    return _M_insert(__after._M_node, __after._M_node, __v);
   516 	} else {
   517 	  // Test for equivalent case
   518 	  if (__comp_v_pos == __comp_pos_v)
   519 	    return __position;
   520 	  else
   521 	    return insert_unique(__v).first;
   522 	}
   523       }
   524   }
   525 }
   526 
   527 
   528 template <class _Key, class _Value, class _KeyOfValue, 
   529           class _Compare, class _Alloc> __iterator__ 
   530 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_equal(iterator __position, const _Value& __v)
   531 {
   532   if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()
   533 
   534     // Check for zero members
   535     if (size() <= 0)
   536         return insert_equal(__v);
   537 
   538     if (!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))
   539       return _M_insert(__position._M_node, __position._M_node, __v);
   540     else    {
   541       // Check for only one member
   542       if (__position._M_node->_M_left == __position._M_node)
   543         // Unlike insert_unique, can't avoid doing a comparison here.
   544         return _M_insert(0, __position._M_node, __v);
   545                 
   546       // All other cases:
   547       // Standard-conformance - does the insertion point fall immediately AFTER
   548       // the hint?
   549       iterator __after = __position;
   550       ++__after;
   551       
   552       // Already know that compare(pos, v) must be true!
   553       // Therefore, we want to know if compare(after, v) is false.
   554       // (i.e., we now pos < v, now we want to know if v <= after)
   555       // If not, invalid hint.
   556       if ( __after._M_node==this->_M_header._M_data ||
   557 	   !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) {
   558         if (_S_right(__position._M_node) == 0)
   559           return _M_insert(0, __position._M_node, __v, __position._M_node);
   560         else
   561           return _M_insert(__after._M_node, __after._M_node, __v);
   562       } else // Invalid hint
   563         return insert_equal(__v);
   564     }
   565   } else if (__position._M_node == this->_M_header._M_data) {// end()
   566     if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
   567       return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null
   568     else
   569       return insert_equal(__v);
   570   } else {
   571     iterator __before = __position;
   572     --__before;
   573     // store the result of the comparison between pos and v so
   574     // that we don't have to do it again later.  Note that this reverses the shortcut
   575     // on the if, possibly harming efficiency in comparisons; I think the harm will
   576     // be negligible, and to do what I want to do (save the result of a comparison so
   577     // that it can be re-used) there is no alternative.  Test here is for before <= v <= pos.
   578     bool __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));
   579     if (!__comp_pos_v
   580         && !_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))) {
   581       if (_S_right(__before._M_node) == 0)
   582         return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null
   583       else
   584         return _M_insert(__position._M_node, __position._M_node, __v);
   585     } else  {
   586       // Does the insertion point fall immediately AFTER the hint?
   587       // Test for pos < v <= after
   588       iterator __after = __position;
   589       ++__after;
   590       
   591       if (__comp_pos_v
   592 	  && ( __after._M_node==this->_M_header._M_data 
   593 	       || !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) ) {
   594         if (_S_right(__position._M_node) == 0)
   595           return _M_insert(0, __position._M_node, __v, __position._M_node);
   596         else
   597           return _M_insert(__after._M_node, __after._M_node, __v);
   598       } else // Invalid hint
   599         return insert_equal(__v);
   600     }
   601   }
   602 }
   603 
   604 template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc> _Rb_tree_node<_Value>* 
   605 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::_M_copy(_Rb_tree_node<_Value>* __x, _Rb_tree_node<_Value>* __p)
   606 {
   607                         // structural copy.  __x and __p must be non-null.
   608   _STLP_LEAVE_VOLATILE _Link_type __top = _M_clone_node(__x);
   609   __top->_M_parent = __p;
   610   
   611   _STLP_TRY {
   612     if (__x->_M_right)
   613       __top->_M_right = _M_copy(_S_right(__x), __top);
   614     __p = __top;
   615     __x = _S_left(__x);
   616 
   617     while (__x != 0) {
   618       _Link_type __y = _M_clone_node(__x);
   619       __p->_M_left = __y;
   620       __y->_M_parent = __p;
   621       if (__x->_M_right)
   622         __y->_M_right = _M_copy(_S_right(__x), __y);
   623       __p = __y;
   624       __x = _S_left(__x);
   625     }
   626   }
   627   _STLP_UNWIND(_M_erase(__top));
   628 
   629   return __top;
   630 }
   631 
   632 // this has to stay out-of-line : it's recursive
   633 template <class _Key, class _Value, class _KeyOfValue, 
   634           class _Compare, class _Alloc> void 
   635 _Rb_tree<_Key,_Value,_KeyOfValue,
   636   _Compare,_Alloc>::_M_erase(_Rb_tree_node<_Value>* __x)
   637 {
   638                                 // erase without rebalancing
   639   while (__x != 0) {
   640     _M_erase(_S_right(__x));
   641     _Link_type __y = _S_left(__x);
   642     _STLP_STD::_Destroy(&__x->_M_value_field);
   643     this->_M_header.deallocate(__x,1);
   644     __x = __y;
   645   }
   646 }
   647 
   648 template <class _Key, class _Value, class _KeyOfValue, 
   649           class _Compare, class _Alloc> __size_type__ 
   650 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::count(const _Key& __k) const
   651 {
   652   pair<const_iterator, const_iterator> __p = equal_range(__k);
   653   size_type __n = distance(__p.first, __p.second);
   654   return __n;
   655 }
   656 
   657 inline int 
   658 __black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root)
   659 {
   660   if (__node == 0)
   661     return 0;
   662   else {
   663     int __bc = __node->_M_color == _S_rb_tree_black ? 1 : 0;
   664     if (__node == __root)
   665       return __bc;
   666     else
   667       return __bc + __black_count(__node->_M_parent, __root);
   668   }
   669 }
   670 
   671 template <class _Key, class _Value, class _KeyOfValue, 
   672           class _Compare, class _Alloc> bool _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
   673 {
   674   if (_M_node_count == 0 || begin() == end())
   675     return _M_node_count == 0 && begin() == end() && this->_M_header._M_data->_M_left == this->_M_header._M_data
   676       && this->_M_header._M_data->_M_right == this->_M_header._M_data;
   677   
   678   int __len = __black_count(_M_leftmost(), _M_root());
   679   for (const_iterator __it = begin(); __it != end(); ++__it) {
   680     _Link_type __x = (_Link_type) __it._M_node;
   681     _Link_type __L = _S_left(__x);
   682     _Link_type __R = _S_right(__x);
   683 
   684     if (__x->_M_color == _S_rb_tree_red)
   685       if ((__L && __L->_M_color == _S_rb_tree_red) ||
   686           (__R && __R->_M_color == _S_rb_tree_red))
   687         return false;
   688 
   689     if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
   690       return false;
   691     if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
   692       return false;
   693 
   694     if (!__L && !__R && __black_count(__x, _M_root()) != __len)
   695       return false;
   696   }
   697 
   698   if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
   699     return false;
   700   if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
   701     return false;
   702 
   703   return true;
   704 }
   705 _STLP_END_NAMESPACE
   706 
   707 # undef __iterator__        
   708 # undef iterator
   709 # undef __size_type__  
   710 
   711 #endif /*  _STLP_TREE_C */
   712 
   713 // Local Variables:
   714 // mode:C++
   715 // End: