home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / cwin / c.exe / $INSTDIR / include / c++ / bits / stl_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-12-15  |  42.8 KB  |  1,463 lines

  1. // RB tree implementation -*- C++ -*-
  2.  
  3. // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library.  This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 2, or (at your option)
  9. // any later version.
  10.  
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15.  
  16. // You should have received a copy of the GNU General Public License along
  17. // with this library; see the file COPYING.  If not, write to the Free
  18. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. // USA.
  20.  
  21. // As a special exception, you may use this file as part of a free software
  22. // library without restriction.  Specifically, if other files instantiate
  23. // templates or use macros or inline functions from this file, or you compile
  24. // this file and link it with other files to produce an executable, this
  25. // file does not by itself cause the resulting executable to be covered by
  26. // the GNU General Public License.  This exception does not however
  27. // invalidate any other reasons why the executable file might be covered by
  28. // the GNU General Public License.
  29.  
  30. /*
  31.  *
  32.  * Copyright (c) 1996,1997
  33.  * Silicon Graphics Computer Systems, Inc.
  34.  *
  35.  * Permission to use, copy, modify, distribute and sell this software
  36.  * and its documentation for any purpose is hereby granted without fee,
  37.  * provided that the above copyright notice appear in all copies and
  38.  * that both that copyright notice and this permission notice appear
  39.  * in supporting documentation.  Silicon Graphics makes no
  40.  * representations about the suitability of this software for any
  41.  * purpose.  It is provided "as is" without express or implied warranty.
  42.  *
  43.  *
  44.  * Copyright (c) 1994
  45.  * Hewlett-Packard Company
  46.  *
  47.  * Permission to use, copy, modify, distribute and sell this software
  48.  * and its documentation for any purpose is hereby granted without fee,
  49.  * provided that the above copyright notice appear in all copies and
  50.  * that both that copyright notice and this permission notice appear
  51.  * in supporting documentation.  Hewlett-Packard Company makes no
  52.  * representations about the suitability of this software for any
  53.  * purpose.  It is provided "as is" without express or implied warranty.
  54.  *
  55.  *
  56.  */
  57.  
  58. /** @file stl_tree.h
  59.  *  This is an internal header file, included by other library headers.
  60.  *  You should not attempt to use it directly.
  61.  */
  62.  
  63. #ifndef __GLIBCPP_INTERNAL_TREE_H
  64. #define __GLIBCPP_INTERNAL_TREE_H
  65.  
  66. /*
  67.  
  68. Red-black tree class, designed for use in implementing STL
  69. associative containers (set, multiset, map, and multimap). The
  70. insertion and deletion algorithms are based on those in Cormen,
  71. Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
  72. except that
  73.  
  74. (1) the header cell is maintained with links not only to the root
  75. but also to the leftmost node of the tree, to enable constant time
  76. begin(), and to the rightmost node of the tree, to enable linear time
  77. performance when used with the generic set algorithms (set_union,
  78. etc.);
  79.  
  80. (2) when a node being deleted has two children its successor node is
  81. relinked into its place, rather than copied, so that the only
  82. iterators invalidated are those referring to the deleted node.
  83.  
  84. */
  85.  
  86. #include <bits/stl_algobase.h>
  87. #include <bits/stl_alloc.h>
  88. #include <bits/stl_construct.h>
  89. #include <bits/stl_function.h>
  90.  
  91. namespace std
  92.   enum _Rb_tree_color { _M_red = false, _M_black = true };
  93.  
  94.   struct _Rb_tree_node_base
  95.   {
  96.     typedef _Rb_tree_node_base* _Base_ptr;
  97.     
  98.     _Rb_tree_color     _M_color; 
  99.     _Base_ptr         _M_parent;
  100.     _Base_ptr         _M_left;
  101.     _Base_ptr         _M_right;
  102.     
  103.     static _Base_ptr 
  104.     _S_minimum(_Base_ptr __x)
  105.     {
  106.       while (__x->_M_left != 0) __x = __x->_M_left;
  107.       return __x;
  108.     }
  109.  
  110.     static _Base_ptr 
  111.     _S_maximum(_Base_ptr __x)
  112.     {
  113.       while (__x->_M_right != 0) __x = __x->_M_right;
  114.       return __x;
  115.     }
  116.   };
  117.  
  118.   template<typename _Val>
  119.     struct _Rb_tree_node : public _Rb_tree_node_base
  120.     {
  121.       typedef _Rb_tree_node<_Val>* _Link_type;
  122.       _Val _M_value_field;
  123.     };
  124.   
  125.   struct _Rb_tree_base_iterator
  126.   {
  127.     typedef _Rb_tree_node_base::_Base_ptr     _Base_ptr;
  128.     typedef bidirectional_iterator_tag         iterator_category;
  129.     typedef ptrdiff_t                 difference_type;
  130.  
  131.     _Base_ptr _M_node;
  132.  
  133.     void 
  134.     _M_increment()
  135.     {
  136.       if (_M_node->_M_right != 0) 
  137.     {
  138.       _M_node = _M_node->_M_right;
  139.       while (_M_node->_M_left != 0)
  140.         _M_node = _M_node->_M_left;
  141.     }
  142.       else 
  143.     {
  144.       _Base_ptr __y = _M_node->_M_parent;
  145.       while (_M_node == __y->_M_right) 
  146.         {
  147.           _M_node = __y;
  148.           __y = __y->_M_parent;
  149.         }
  150.       if (_M_node->_M_right != __y)
  151.         _M_node = __y;
  152.     }
  153.     }
  154.  
  155.     void 
  156.     _M_decrement()
  157.     {
  158.       if (_M_node->_M_color == _M_red 
  159.       && _M_node->_M_parent->_M_parent == _M_node)
  160.     _M_node = _M_node->_M_right;
  161.       else if (_M_node->_M_left != 0) 
  162.     {
  163.       _Base_ptr __y = _M_node->_M_left;
  164.       while (__y->_M_right != 0)
  165.         __y = __y->_M_right;
  166.       _M_node = __y;
  167.     }
  168.       else 
  169.     {
  170.       _Base_ptr __y = _M_node->_M_parent;
  171.       while (_M_node == __y->_M_left) 
  172.         {
  173.           _M_node = __y;
  174.           __y = __y->_M_parent;
  175.         }
  176.       _M_node = __y;
  177.     }
  178.     }
  179.   };
  180.  
  181.   template<typename _Val, typename _Ref, typename _Ptr>
  182.     struct _Rb_tree_iterator : public _Rb_tree_base_iterator
  183.     {
  184.       typedef _Val value_type;
  185.       typedef _Ref reference;
  186.       typedef _Ptr pointer;
  187.       typedef _Rb_tree_iterator<_Val, _Val&, _Val*> iterator;
  188.       typedef _Rb_tree_iterator<_Val, const _Val&, const _Val*> 
  189.       const_iterator;
  190.       typedef _Rb_tree_iterator<_Val, _Ref, _Ptr> _Self;
  191.       typedef _Rb_tree_node<_Val>* _Link_type;
  192.       
  193.       _Rb_tree_iterator() {}
  194.       _Rb_tree_iterator(_Link_type __x) { _M_node = __x; }
  195.       _Rb_tree_iterator(const iterator& __it) { _M_node = __it._M_node; }
  196.  
  197.       reference 
  198.       operator*() const { return _Link_type(_M_node)->_M_value_field; }
  199.  
  200.       pointer 
  201.       operator->() const { return &(operator*()); }
  202.  
  203.       _Self& 
  204.       operator++() 
  205.       { 
  206.     _M_increment(); 
  207.     return *this; 
  208.       }
  209.  
  210.       _Self 
  211.       operator++(int) 
  212.       {
  213.     _Self __tmp = *this;
  214.     _M_increment();
  215.     return __tmp;
  216.       }
  217.     
  218.       _Self& 
  219.       operator--() { _M_decrement(); return *this; }
  220.  
  221.       _Self 
  222.       operator--(int) 
  223.       {
  224.     _Self __tmp = *this;
  225.     _M_decrement();
  226.     return __tmp;
  227.       }
  228.   };
  229.  
  230.   template<typename _Val, typename _Ref, typename _Ptr>
  231.     inline bool 
  232.     operator==(const _Rb_tree_iterator<_Val, _Ref, _Ptr>& __x,
  233.            const _Rb_tree_iterator<_Val, _Ref, _Ptr>& __y) 
  234.     { return __x._M_node == __y._M_node; }
  235.  
  236.   template<typename _Val>
  237.     inline bool 
  238.     operator==(const _Rb_tree_iterator<_Val, const _Val&, const _Val*>& __x,
  239.            const _Rb_tree_iterator<_Val, _Val&, _Val*>& __y) 
  240.     { return __x._M_node == __y._M_node; }
  241.  
  242.   template<typename _Val>
  243.     inline bool 
  244.     operator==(const _Rb_tree_iterator<_Val, _Val&, _Val*>& __x,
  245.            const _Rb_tree_iterator<_Val, const _Val&, const _Val*>& __y) 
  246.     { return __x._M_node == __y._M_node; }
  247.  
  248.   template<typename _Val, typename _Ref, typename _Ptr>
  249.     inline bool 
  250.     operator!=(const _Rb_tree_iterator<_Val, _Ref, _Ptr>& __x,
  251.            const _Rb_tree_iterator<_Val, _Ref, _Ptr>& __y) 
  252.     { return __x._M_node != __y._M_node; }
  253.  
  254.   template<typename _Val>
  255.     inline bool 
  256.     operator!=(const _Rb_tree_iterator<_Val, const _Val&, const _Val*>& __x,
  257.            const _Rb_tree_iterator<_Val, _Val&, _Val*>& __y) 
  258.     { return __x._M_node != __y._M_node; }
  259.  
  260.   template<typename _Val>
  261.     inline bool 
  262.     operator!=(const _Rb_tree_iterator<_Val, _Val&, _Val*>& __x,
  263.            const _Rb_tree_iterator<_Val, const _Val&, const _Val*>& __y) 
  264.     { return __x._M_node != __y._M_node; }
  265.  
  266.   inline void 
  267.   _Rb_tree_rotate_left(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
  268.   {
  269.     _Rb_tree_node_base* __y = __x->_M_right;
  270.     __x->_M_right = __y->_M_left;
  271.     if (__y->_M_left !=0)
  272.       __y->_M_left->_M_parent = __x;
  273.     __y->_M_parent = __x->_M_parent;
  274.     
  275.     if (__x == __root)
  276.       __root = __y;
  277.     else if (__x == __x->_M_parent->_M_left)
  278.       __x->_M_parent->_M_left = __y;
  279.     else
  280.       __x->_M_parent->_M_right = __y;
  281.     __y->_M_left = __x;
  282.     __x->_M_parent = __y;
  283.   }
  284.  
  285.   inline void 
  286.   _Rb_tree_rotate_right(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
  287.   {
  288.     _Rb_tree_node_base* __y = __x->_M_left;
  289.     __x->_M_left = __y->_M_right;
  290.     if (__y->_M_right != 0)
  291.       __y->_M_right->_M_parent = __x;
  292.     __y->_M_parent = __x->_M_parent;
  293.  
  294.     if (__x == __root)
  295.       __root = __y;
  296.     else if (__x == __x->_M_parent->_M_right)
  297.       __x->_M_parent->_M_right = __y;
  298.     else
  299.       __x->_M_parent->_M_left = __y;
  300.     __y->_M_right = __x;
  301.     __x->_M_parent = __y;
  302.   }
  303.  
  304.   inline void 
  305.   _Rb_tree_rebalance(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
  306.   {
  307.     __x->_M_color = _M_red;
  308.     while (__x != __root 
  309.        && __x->_M_parent->_M_color == _M_red) 
  310.       {
  311.     if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) 
  312.       {
  313.         _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
  314.         if (__y && __y->_M_color == _M_red) 
  315.           {
  316.         __x->_M_parent->_M_color = _M_black;
  317.         __y->_M_color = _M_black;
  318.         __x->_M_parent->_M_parent->_M_color = _M_red;
  319.         __x = __x->_M_parent->_M_parent;
  320.           }
  321.         else 
  322.           {
  323.         if (__x == __x->_M_parent->_M_right) 
  324.           {
  325.             __x = __x->_M_parent;
  326.             _Rb_tree_rotate_left(__x, __root);
  327.           }
  328.         __x->_M_parent->_M_color = _M_black;
  329.         __x->_M_parent->_M_parent->_M_color = _M_red;
  330.         _Rb_tree_rotate_right(__x->_M_parent->_M_parent, __root);
  331.           }
  332.       }
  333.     else 
  334.       {
  335.         _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
  336.         if (__y && __y->_M_color == _M_red) 
  337.           {
  338.         __x->_M_parent->_M_color = _M_black;
  339.         __y->_M_color = _M_black;
  340.         __x->_M_parent->_M_parent->_M_color = _M_red;
  341.         __x = __x->_M_parent->_M_parent;
  342.           }
  343.         else 
  344.           {
  345.         if (__x == __x->_M_parent->_M_left) 
  346.           {
  347.             __x = __x->_M_parent;
  348.             _Rb_tree_rotate_right(__x, __root);
  349.           }
  350.         __x->_M_parent->_M_color = _M_black;
  351.         __x->_M_parent->_M_parent->_M_color = _M_red;
  352.         _Rb_tree_rotate_left(__x->_M_parent->_M_parent, __root);
  353.           }
  354.       }
  355.       }
  356.     __root->_M_color = _M_black;
  357.   }
  358.  
  359.   inline _Rb_tree_node_base*
  360.   _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* __z, 
  361.                    _Rb_tree_node_base*& __root,
  362.                    _Rb_tree_node_base*& __leftmost,
  363.                    _Rb_tree_node_base*& __rightmost)
  364.   {
  365.     _Rb_tree_node_base* __y = __z;
  366.     _Rb_tree_node_base* __x = 0;
  367.     _Rb_tree_node_base* __x_parent = 0;
  368.     if (__y->_M_left == 0)     // __z has at most one non-null child. y == z.
  369.       __x = __y->_M_right;     // __x might be null.
  370.     else
  371.       if (__y->_M_right == 0)  // __z has exactly one non-null child. y == z.
  372.     __x = __y->_M_left;    // __x is not null.
  373.       else 
  374.     {
  375.       // __z has two non-null children.  Set __y to
  376.       __y = __y->_M_right;   //   __z's successor.  __x might be null.
  377.       while (__y->_M_left != 0)
  378.         __y = __y->_M_left;
  379.       __x = __y->_M_right;
  380.     }
  381.     if (__y != __z) 
  382.       {
  383.     // relink y in place of z.  y is z's successor
  384.     __z->_M_left->_M_parent = __y; 
  385.     __y->_M_left = __z->_M_left;
  386.     if (__y != __z->_M_right) 
  387.       {
  388.         __x_parent = __y->_M_parent;
  389.         if (__x) __x->_M_parent = __y->_M_parent;
  390.         __y->_M_parent->_M_left = __x;   // __y must be a child of _M_left
  391.         __y->_M_right = __z->_M_right;
  392.         __z->_M_right->_M_parent = __y;
  393.       }
  394.     else
  395.       __x_parent = __y;  
  396.     if (__root == __z)
  397.       __root = __y;
  398.     else if (__z->_M_parent->_M_left == __z)
  399.       __z->_M_parent->_M_left = __y;
  400.     else 
  401.       __z->_M_parent->_M_right = __y;
  402.     __y->_M_parent = __z->_M_parent;
  403.     std::swap(__y->_M_color, __z->_M_color);
  404.     __y = __z;
  405.     // __y now points to node to be actually deleted
  406.       }
  407.     else 
  408.       {                        // __y == __z
  409.     __x_parent = __y->_M_parent;
  410.     if (__x) 
  411.       __x->_M_parent = __y->_M_parent;   
  412.     if (__root == __z)
  413.       __root = __x;
  414.     else 
  415.       if (__z->_M_parent->_M_left == __z)
  416.         __z->_M_parent->_M_left = __x;
  417.       else
  418.         __z->_M_parent->_M_right = __x;
  419.     if (__leftmost == __z) 
  420.       if (__z->_M_right == 0)        // __z->_M_left must be null also
  421.         __leftmost = __z->_M_parent;
  422.     // makes __leftmost == _M_header if __z == __root
  423.       else
  424.         __leftmost = _Rb_tree_node_base::_S_minimum(__x);
  425.     if (__rightmost == __z)  
  426.       if (__z->_M_left == 0)         // __z->_M_right must be null also
  427.         __rightmost = __z->_M_parent;  
  428.     // makes __rightmost == _M_header if __z == __root
  429.       else                      // __x == __z->_M_left
  430.         __rightmost = _Rb_tree_node_base::_S_maximum(__x);
  431.       }
  432.     if (__y->_M_color != _M_red) 
  433.       { 
  434.     while (__x != __root && (__x == 0 || __x->_M_color == _M_black))
  435.       if (__x == __x_parent->_M_left) 
  436.         {
  437.           _Rb_tree_node_base* __w = __x_parent->_M_right;
  438.           if (__w->_M_color == _M_red) 
  439.         {
  440.           __w->_M_color = _M_black;
  441.           __x_parent->_M_color = _M_red;
  442.           _Rb_tree_rotate_left(__x_parent, __root);
  443.           __w = __x_parent->_M_right;
  444.         }
  445.           if ((__w->_M_left == 0 || 
  446.            __w->_M_left->_M_color == _M_black) &&
  447.           (__w->_M_right == 0 || 
  448.            __w->_M_right->_M_color == _M_black)) 
  449.         {
  450.           __w->_M_color = _M_red;
  451.           __x = __x_parent;
  452.           __x_parent = __x_parent->_M_parent;
  453.         } 
  454.           else 
  455.         {
  456.           if (__w->_M_right == 0 
  457.               || __w->_M_right->_M_color == _M_black) 
  458.             {
  459.               if (__w->_M_left) __w->_M_left->_M_color = _M_black;
  460.               __w->_M_color = _M_red;
  461.               _Rb_tree_rotate_right(__w, __root);
  462.               __w = __x_parent->_M_right;
  463.             }
  464.           __w->_M_color = __x_parent->_M_color;
  465.           __x_parent->_M_color = _M_black;
  466.           if (__w->_M_right) 
  467.             __w->_M_right->_M_color = _M_black;
  468.           _Rb_tree_rotate_left(__x_parent, __root);
  469.           break;
  470.         }
  471.         } 
  472.       else 
  473.         {   
  474.           // same as above, with _M_right <-> _M_left.
  475.           _Rb_tree_node_base* __w = __x_parent->_M_left;
  476.           if (__w->_M_color == _M_red) 
  477.         {
  478.           __w->_M_color = _M_black;
  479.           __x_parent->_M_color = _M_red;
  480.           _Rb_tree_rotate_right(__x_parent, __root);
  481.           __w = __x_parent->_M_left;
  482.         }
  483.           if ((__w->_M_right == 0 || 
  484.            __w->_M_right->_M_color == _M_black) &&
  485.           (__w->_M_left == 0 || 
  486.            __w->_M_left->_M_color == _M_black)) 
  487.         {
  488.           __w->_M_color = _M_red;
  489.           __x = __x_parent;
  490.           __x_parent = __x_parent->_M_parent;
  491.         } 
  492.           else 
  493.         {
  494.           if (__w->_M_left == 0 || __w->_M_left->_M_color == _M_black) 
  495.             {
  496.               if (__w->_M_right) __w->_M_right->_M_color = _M_black;
  497.               __w->_M_color = _M_red;
  498.               _Rb_tree_rotate_left(__w, __root);
  499.               __w = __x_parent->_M_left;
  500.             }
  501.           __w->_M_color = __x_parent->_M_color;
  502.           __x_parent->_M_color = _M_black;
  503.           if (__w->_M_left) 
  504.             __w->_M_left->_M_color = _M_black;
  505.           _Rb_tree_rotate_right(__x_parent, __root);
  506.           break;
  507.         }
  508.         }
  509.     if (__x) __x->_M_color = _M_black;
  510.       }
  511.     return __y;
  512.   }
  513.  
  514.   // Base class to encapsulate the differences between old SGI-style
  515.   // allocators and standard-conforming allocators.  In order to avoid
  516.   // having an empty base class, we arbitrarily move one of rb_tree's
  517.   // data members into the base class.
  518.  
  519.   // _Base for general standard-conforming allocators.
  520.   template<typename _Tp, typename _Alloc, bool _S_instanceless>
  521.     class _Rb_tree_alloc_base 
  522.     {
  523.     public:
  524.     typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
  525.  
  526.       allocator_type 
  527.       get_allocator() const { return _M_node_allocator; }
  528.  
  529.       _Rb_tree_alloc_base(const allocator_type& __a)
  530.       : _M_node_allocator(__a), _M_header(0) {}
  531.  
  532.     protected:
  533.       typename _Alloc_traits<_Rb_tree_node<_Tp>, _Alloc>::allocator_type
  534.       _M_node_allocator;
  535.  
  536.       _Rb_tree_node<_Tp>* _M_header;
  537.       
  538.       _Rb_tree_node<_Tp>* 
  539.       _M_get_node()  { return _M_node_allocator.allocate(1); }
  540.  
  541.       void 
  542.       _M_put_node(_Rb_tree_node<_Tp>* __p) 
  543.       { _M_node_allocator.deallocate(__p, 1); }
  544.     };
  545.  
  546.   // Specialization for instanceless allocators.
  547.   template<typename _Tp, typename _Alloc>
  548.     class _Rb_tree_alloc_base<_Tp, _Alloc, true> 
  549.     {
  550.     public:
  551.     typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
  552.       allocator_type get_allocator() const { return allocator_type(); }
  553.  
  554.       _Rb_tree_alloc_base(const allocator_type&) : _M_header(0) {}
  555.  
  556.     protected:
  557.       _Rb_tree_node<_Tp>* _M_header;
  558.       
  559.       typedef typename _Alloc_traits<_Rb_tree_node<_Tp>, _Alloc>::_Alloc_type
  560.       _Alloc_type;
  561.       
  562.       _Rb_tree_node<_Tp>* 
  563.       _M_get_node() { return _Alloc_type::allocate(1); }
  564.  
  565.       void 
  566.       _M_put_node(_Rb_tree_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
  567.     };
  568.   
  569.   template<typename _Tp, typename _Alloc>
  570.     struct _Rb_tree_base : public _Rb_tree_alloc_base<_Tp, _Alloc, 
  571.                                   _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
  572.     {
  573.       typedef _Rb_tree_alloc_base<_Tp, 
  574.     _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base;
  575.       typedef typename _Base::allocator_type allocator_type;
  576.  
  577.       _Rb_tree_base(const allocator_type& __a) 
  578.       : _Base(__a) { _M_header = _M_get_node(); }
  579.       ~_Rb_tree_base() { _M_put_node(_M_header); }
  580.     };
  581.  
  582.  
  583.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  584.            typename _Compare, typename _Alloc = allocator<_Val> >
  585.     class _Rb_tree : protected _Rb_tree_base<_Val, _Alloc> 
  586.     {
  587.       typedef _Rb_tree_base<_Val, _Alloc> _Base;
  588.       
  589.     protected:
  590.       typedef _Rb_tree_node_base* _Base_ptr;
  591.       typedef _Rb_tree_node<_Val> _Rb_tree_node;
  592.       
  593.     public:
  594.       typedef _Key key_type;
  595.       typedef _Val value_type;
  596.       typedef value_type* pointer;
  597.       typedef const value_type* const_pointer;
  598.       typedef value_type& reference;
  599.       typedef const value_type& const_reference;
  600.       typedef _Rb_tree_node* _Link_type;
  601.       typedef size_t size_type;
  602.       typedef ptrdiff_t difference_type;
  603.       
  604.       typedef typename _Base::allocator_type allocator_type;
  605.       allocator_type get_allocator() const { return _Base::get_allocator(); }
  606.       
  607.     protected:
  608.       using _Base::_M_get_node;
  609.       using _Base::_M_put_node;
  610.       using _Base::_M_header;
  611.       
  612.       _Link_type
  613.       _M_create_node(const value_type& __x)
  614.       {
  615.     _Link_type __tmp = _M_get_node();
  616.     try 
  617.       { _Construct(&__tmp->_M_value_field, __x); }
  618.     catch(...)
  619.       {
  620.       _M_put_node(__tmp);
  621.       __throw_exception_again; 
  622.       }
  623.     return __tmp;
  624.       }
  625.       
  626.       _Link_type 
  627.       _M_clone_node(_Link_type __x)
  628.       {
  629.     _Link_type __tmp = _M_create_node(__x->_M_value_field);
  630.     __tmp->_M_color = __x->_M_color;
  631.     __tmp->_M_left = 0;
  632.     __tmp->_M_right = 0;
  633.     return __tmp;
  634.       }
  635.  
  636.       void
  637.       destroy_node(_Link_type __p)
  638.       {
  639.     _Destroy(&__p->_M_value_field);
  640.     _M_put_node(__p);
  641.       }
  642.  
  643.       size_type _M_node_count; // keeps track of size of tree
  644.       _Compare _M_key_compare;
  645.  
  646.       _Link_type& 
  647.       _M_root() const { return (_Link_type&) _M_header->_M_parent; }
  648.  
  649.       _Link_type& 
  650.       _M_leftmost() const { return (_Link_type&) _M_header->_M_left; }
  651.  
  652.       _Link_type& 
  653.       _M_rightmost() const { return (_Link_type&) _M_header->_M_right; }
  654.  
  655.       static _Link_type& 
  656.       _S_left(_Link_type __x) { return (_Link_type&)(__x->_M_left); }
  657.  
  658.       static _Link_type& 
  659.       _S_right(_Link_type __x) { return (_Link_type&)(__x->_M_right); }
  660.  
  661.       static _Link_type& 
  662.       _S_parent(_Link_type __x) { return (_Link_type&)(__x->_M_parent); }
  663.  
  664.       static reference 
  665.       _S_value(_Link_type __x) { return __x->_M_value_field; }
  666.  
  667.       static const _Key& 
  668.       _S_key(_Link_type __x) { return _KeyOfValue()(_S_value(__x)); }
  669.  
  670.       static _Rb_tree_color& 
  671.       _S_color(_Link_type __x) { return __x->_M_color; }
  672.  
  673.       static _Link_type& 
  674.       _S_left(_Base_ptr __x) { return (_Link_type&)(__x->_M_left); }
  675.  
  676.       static _Link_type& 
  677.       _S_right(_Base_ptr __x) { return (_Link_type&)(__x->_M_right); }
  678.  
  679.       static _Link_type& 
  680.       _S_parent(_Base_ptr __x) { return (_Link_type&)(__x->_M_parent); }
  681.  
  682.       static reference 
  683.       _S_value(_Base_ptr __x) { return ((_Link_type)__x)->_M_value_field; }
  684.  
  685.       static const _Key& 
  686.       _S_key(_Base_ptr __x) { return _KeyOfValue()(_S_value(_Link_type(__x)));} 
  687.  
  688.       static _Rb_tree_color&
  689.       _S_color(_Base_ptr __x) { return (_Link_type(__x)->_M_color); }
  690.  
  691.       static _Link_type 
  692.       _S_minimum(_Link_type __x) 
  693.       { return (_Link_type)  _Rb_tree_node_base::_S_minimum(__x); }
  694.  
  695.       static _Link_type 
  696.       _S_maximum(_Link_type __x)
  697.       { return (_Link_type) _Rb_tree_node_base::_S_maximum(__x); }
  698.  
  699.     public:
  700.       typedef _Rb_tree_iterator<value_type, reference, pointer> iterator;
  701.       typedef _Rb_tree_iterator<value_type, const_reference, const_pointer> 
  702.       const_iterator;
  703.  
  704.       typedef reverse_iterator<const_iterator> const_reverse_iterator;
  705.       typedef reverse_iterator<iterator> reverse_iterator;
  706.  
  707.     private:
  708.       iterator 
  709.       _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
  710.  
  711.       _Link_type 
  712.       _M_copy(_Link_type __x, _Link_type __p);
  713.  
  714.       void 
  715.       _M_erase(_Link_type __x);
  716.  
  717.     public:
  718.       // allocation/deallocation
  719.       _Rb_tree()
  720.     : _Base(allocator_type()), _M_node_count(0), _M_key_compare()
  721.       { _M_empty_initialize(); }
  722.  
  723.       _Rb_tree(const _Compare& __comp)
  724.     : _Base(allocator_type()), _M_node_count(0), _M_key_compare(__comp) 
  725.       { _M_empty_initialize(); }
  726.  
  727.       _Rb_tree(const _Compare& __comp, const allocator_type& __a)
  728.     : _Base(__a), _M_node_count(0), _M_key_compare(__comp) 
  729.       { _M_empty_initialize(); }
  730.  
  731.       _Rb_tree(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x) 
  732.     : _Base(__x.get_allocator()), _M_node_count(0), 
  733.          _M_key_compare(__x._M_key_compare)
  734.       { 
  735.     if (__x._M_root() == 0)
  736.       _M_empty_initialize();
  737.     else 
  738.       {
  739.         _S_color(_M_header) = _M_red;
  740.         _M_root() = _M_copy(__x._M_root(), _M_header);
  741.         _M_leftmost() = _S_minimum(_M_root());
  742.         _M_rightmost() = _S_maximum(_M_root());
  743.       }
  744.     _M_node_count = __x._M_node_count;
  745.       }
  746.  
  747.       ~_Rb_tree() { clear(); }
  748.  
  749.       _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& 
  750.       operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x);
  751.  
  752.     private:
  753.       void _M_empty_initialize() 
  754.       {
  755.     _S_color(_M_header) = _M_red; // used to distinguish header from 
  756.     // __root, in iterator.operator++
  757.     _M_root() = 0;
  758.     _M_leftmost() = _M_header;
  759.     _M_rightmost() = _M_header;
  760.       }
  761.  
  762.     public:    
  763.       // Accessors.
  764.       _Compare 
  765.       key_comp() const { return _M_key_compare; }
  766.  
  767.       iterator 
  768.       begin() { return _M_leftmost(); }
  769.  
  770.       const_iterator 
  771.       begin() const { return _M_leftmost(); }
  772.  
  773.       iterator 
  774.       end() { return _M_header; }
  775.  
  776.       const_iterator 
  777.       end() const { return _M_header; }
  778.  
  779.       reverse_iterator 
  780.       rbegin() { return reverse_iterator(end()); }
  781.  
  782.       const_reverse_iterator 
  783.       rbegin() const { return const_reverse_iterator(end()); }
  784.  
  785.       reverse_iterator 
  786.       rend() { return reverse_iterator(begin()); }
  787.  
  788.       const_reverse_iterator 
  789.       rend() const { return const_reverse_iterator(begin()); }
  790.  
  791.       bool 
  792.       empty() const { return _M_node_count == 0; }
  793.  
  794.       size_type 
  795.       size() const { return _M_node_count; }
  796.  
  797.       size_type 
  798.       max_size() const { return size_type(-1); }
  799.  
  800.       void 
  801.       swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t) 
  802.       {
  803.     std::swap(_M_header, __t._M_header);
  804.     std::swap(_M_node_count, __t._M_node_count);
  805.     std::swap(_M_key_compare, __t._M_key_compare);
  806.       }
  807.     
  808.       // Insert/erase.
  809.       pair<iterator,bool> 
  810.       insert_unique(const value_type& __x);
  811.  
  812.       iterator 
  813.       insert_equal(const value_type& __x);
  814.  
  815.       iterator 
  816.       insert_unique(iterator __position, const value_type& __x);
  817.  
  818.       iterator 
  819.       insert_equal(iterator __position, const value_type& __x);
  820.  
  821.       template<typename _InputIterator>
  822.       void 
  823.       insert_unique(_InputIterator __first, _InputIterator __last);
  824.  
  825.       template<typename _InputIterator>
  826.       void 
  827.       insert_equal(_InputIterator __first, _InputIterator __last);
  828.  
  829.       void 
  830.       erase(iterator __position);
  831.  
  832.       size_type 
  833.       erase(const key_type& __x);
  834.  
  835.       void 
  836.       erase(iterator __first, iterator __last);
  837.  
  838.       void 
  839.       erase(const key_type* __first, const key_type* __last);
  840.  
  841.       void 
  842.       clear() 
  843.       {
  844.     if (_M_node_count != 0) 
  845.       {
  846.         _M_erase(_M_root());
  847.         _M_leftmost() = _M_header;
  848.         _M_root() = 0;
  849.         _M_rightmost() = _M_header;
  850.         _M_node_count = 0;
  851.       }
  852.       }      
  853.  
  854.       // Set operations.
  855.       iterator 
  856.       find(const key_type& __x);
  857.  
  858.       const_iterator 
  859.       find(const key_type& __x) const;
  860.  
  861.       size_type 
  862.       count(const key_type& __x) const;
  863.  
  864.       iterator 
  865.       lower_bound(const key_type& __x);
  866.  
  867.       const_iterator 
  868.       lower_bound(const key_type& __x) const;
  869.  
  870.       iterator 
  871.       upper_bound(const key_type& __x);
  872.  
  873.       const_iterator 
  874.       upper_bound(const key_type& __x) const;
  875.  
  876.       pair<iterator,iterator> 
  877.       equal_range(const key_type& __x);
  878.  
  879.       pair<const_iterator, const_iterator> 
  880.       equal_range(const key_type& __x) const;
  881.  
  882.       // Debugging.
  883.       bool 
  884.       __rb_verify() const;
  885.     };
  886.  
  887.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  888.            typename _Compare, typename _Alloc>
  889.     inline bool 
  890.     operator==(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  891.            const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
  892.     {
  893.       return __x.size() == __y.size() && 
  894.     equal(__x.begin(), __x.end(), __y.begin());
  895.     }
  896.  
  897.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  898.            typename _Compare, typename _Alloc>
  899.     inline bool 
  900.     operator<(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  901.           const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
  902.     {
  903.       return lexicographical_compare(__x.begin(), __x.end(),
  904.                      __y.begin(), __y.end());
  905.     }
  906.  
  907.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  908.            typename _Compare, typename _Alloc>
  909.     inline bool 
  910.     operator!=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  911.            const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y) 
  912.     { return !(__x == __y); }
  913.  
  914.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  915.            typename _Compare, typename _Alloc>
  916.     inline bool 
  917.     operator>(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  918.           const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y) 
  919.     { return __y < __x; }
  920.  
  921.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  922.            typename _Compare, typename _Alloc>
  923.     inline bool 
  924.     operator<=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  925.            const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y) 
  926.   { return !(__y < __x); }
  927.  
  928.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  929.            typename _Compare, typename _Alloc>
  930.     inline bool 
  931.     operator>=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  932.            const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y) 
  933.   { return !(__x < __y); }
  934.  
  935.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  936.            typename _Compare, typename _Alloc>
  937.     inline void 
  938.     swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, 
  939.      _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
  940.     { __x.swap(__y); }
  941.  
  942.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  943.            typename _Compare, typename _Alloc>
  944.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& 
  945.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  946.     operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x)
  947.     {
  948.       if (this != &__x) 
  949.     {
  950.       // Note that _Key may be a constant type.
  951.       clear();
  952.       _M_node_count = 0;
  953.       _M_key_compare = __x._M_key_compare;        
  954.       if (__x._M_root() == 0) 
  955.         {
  956.           _M_root() = 0;
  957.           _M_leftmost() = _M_header;
  958.           _M_rightmost() = _M_header;
  959.         }
  960.       else 
  961.         {
  962.           _M_root() = _M_copy(__x._M_root(), _M_header);
  963.           _M_leftmost() = _S_minimum(_M_root());
  964.           _M_rightmost() = _S_maximum(_M_root());
  965.           _M_node_count = __x._M_node_count;
  966.         }
  967.     }
  968.       return *this;
  969.     }
  970.  
  971.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  972.            typename _Compare, typename _Alloc>
  973.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
  974.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  975.     _M_insert(_Base_ptr __x_, _Base_ptr __y_, const _Val& __v)
  976.     {
  977.       _Link_type __x = (_Link_type) __x_;
  978.       _Link_type __y = (_Link_type) __y_;
  979.       _Link_type __z;
  980.       
  981.       if (__y == _M_header || __x != 0 || 
  982.       _M_key_compare(_KeyOfValue()(__v), _S_key(__y))) 
  983.     {
  984.       __z = _M_create_node(__v);
  985.       _S_left(__y) = __z;               // also makes _M_leftmost() = __z 
  986.       //    when __y == _M_header
  987.       if (__y == _M_header) 
  988.         {
  989.           _M_root() = __z;
  990.           _M_rightmost() = __z;
  991.         }
  992.       else if (__y == _M_leftmost())
  993.         _M_leftmost() = __z; // maintain _M_leftmost() pointing to min node
  994.     }
  995.       else 
  996.     {
  997.       __z = _M_create_node(__v);
  998.       _S_right(__y) = __z;
  999.       // Maintain _M_rightmost() pointing to max node.
  1000.       if (__y == _M_rightmost())
  1001.         _M_rightmost() = __z; 
  1002.     }
  1003.       _S_parent(__z) = __y;
  1004.       _S_left(__z) = 0;
  1005.       _S_right(__z) = 0;
  1006.       _Rb_tree_rebalance(__z, _M_header->_M_parent);
  1007.       ++_M_node_count;
  1008.       return iterator(__z);
  1009.     }
  1010.  
  1011.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1012.            typename _Compare, typename _Alloc>
  1013.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
  1014.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1015.     insert_equal(const _Val& __v)
  1016.     {
  1017.       _Link_type __y = _M_header;
  1018.       _Link_type __x = _M_root();
  1019.       while (__x != 0) 
  1020.     {
  1021.       __y = __x;
  1022.       __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ? 
  1023.         _S_left(__x) : _S_right(__x);
  1024.     }
  1025.       return _M_insert(__x, __y, __v);
  1026.     }
  1027.  
  1028.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1029.            typename _Compare, typename _Alloc>
  1030.     pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator, 
  1031.     bool>
  1032.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1033.     insert_unique(const _Val& __v)
  1034.     {
  1035.       _Link_type __y = _M_header;
  1036.       _Link_type __x = _M_root();
  1037.       bool __comp = true;
  1038.       while (__x != 0) 
  1039.     {
  1040.       __y = __x;
  1041.       __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
  1042.       __x = __comp ? _S_left(__x) : _S_right(__x);
  1043.     }
  1044.       iterator __j = iterator(__y);   
  1045.       if (__comp)
  1046.     if (__j == begin())     
  1047.       return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
  1048.     else
  1049.       --__j;
  1050.       if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
  1051.     return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
  1052.       return pair<iterator,bool>(__j, false);
  1053.     }
  1054.   
  1055.  
  1056.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1057.            typename _Compare, typename _Alloc>
  1058.     typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator 
  1059.     _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1060.     insert_unique(iterator __position, const _Val& __v)
  1061.     {
  1062.       if (__position._M_node == _M_header->_M_left) 
  1063.     { 
  1064.       // begin()
  1065.       if (size() > 0 && 
  1066.           _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
  1067.         return _M_insert(__position._M_node, __position._M_node, __v);
  1068.       // first argument just needs to be non-null 
  1069.       else
  1070.         return insert_unique(__v).first;
  1071.     } 
  1072.       else if (__position._M_node == _M_header) 
  1073.     { 
  1074.       // end()
  1075.       if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
  1076.         return _M_insert(0, _M_rightmost(), __v);
  1077.       else
  1078.         return insert_unique(__v).first;
  1079.     } 
  1080.       else 
  1081.     {
  1082.       iterator __before = __position;
  1083.       --__before;
  1084.       if (_M_key_compare(_S_key(__before._M_node), _KeyOfValue()(__v)) 
  1085.           && _M_key_compare(_KeyOfValue()(__v),_S_key(__position._M_node)))
  1086.         {
  1087.           if (_S_right(__before._M_node) == 0)
  1088.         return _M_insert(0, __before._M_node, __v); 
  1089.           else
  1090.         return _M_insert(__position._M_node, __position._M_node, __v);
  1091.           // first argument just needs to be non-null 
  1092.         } 
  1093.       else
  1094.         return insert_unique(__v).first;
  1095.     }
  1096.     }
  1097.  
  1098.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1099.            typename _Compare, typename _Alloc>
  1100.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator 
  1101.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1102.     insert_equal(iterator __position, const _Val& __v)
  1103.     {
  1104.       if (__position._M_node == _M_header->_M_left) 
  1105.     { 
  1106.       // begin()
  1107.       if (size() > 0 && 
  1108.           !_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))
  1109.         return _M_insert(__position._M_node, __position._M_node, __v);
  1110.       // first argument just needs to be non-null 
  1111.       else
  1112.         return insert_equal(__v);
  1113.     } 
  1114.       else if (__position._M_node == _M_header) 
  1115.     {
  1116.       // end()
  1117.       if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
  1118.         return _M_insert(0, _M_rightmost(), __v);
  1119.       else
  1120.         return insert_equal(__v);
  1121.     } 
  1122.       else 
  1123.     {
  1124.       iterator __before = __position;
  1125.       --__before;
  1126.       if (!_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))
  1127.           && !_M_key_compare(_S_key(__position._M_node),
  1128.                  _KeyOfValue()(__v))) 
  1129.         {
  1130.           if (_S_right(__before._M_node) == 0)
  1131.         return _M_insert(0, __before._M_node, __v); 
  1132.           else
  1133.         return _M_insert(__position._M_node, __position._M_node, __v);
  1134.           // first argument just needs to be non-null 
  1135.         } 
  1136.       else
  1137.         return insert_equal(__v);
  1138.     }
  1139.     }
  1140.  
  1141.   template<typename _Key, typename _Val, typename _KoV, 
  1142.            typename _Cmp, typename _Alloc>
  1143.     template<class _II>
  1144.       void 
  1145.       _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
  1146.       insert_equal(_II __first, _II __last)
  1147.       {
  1148.     for ( ; __first != __last; ++__first)
  1149.       insert_equal(*__first);
  1150.       }
  1151.  
  1152.   template<typename _Key, typename _Val, typename _KoV, 
  1153.            typename _Cmp, typename _Alloc> 
  1154.     template<class _II>
  1155.     void 
  1156.     _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
  1157.     insert_unique(_II __first, _II __last) 
  1158.     {
  1159.       for ( ; __first != __last; ++__first)
  1160.     insert_unique(*__first);
  1161.     }
  1162.  
  1163.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1164.            typename _Compare, typename _Alloc>
  1165.     inline void 
  1166.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(iterator __position)
  1167.     {
  1168.       _Link_type __y = 
  1169.     (_Link_type) _Rb_tree_rebalance_for_erase(__position._M_node,
  1170.                           _M_header->_M_parent,
  1171.                           _M_header->_M_left,
  1172.                           _M_header->_M_right);
  1173.       destroy_node(__y);
  1174.       --_M_node_count;
  1175.     }
  1176.  
  1177.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1178.            typename _Compare, typename _Alloc>
  1179.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type 
  1180.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(const _Key& __x)
  1181.     {
  1182.       pair<iterator,iterator> __p = equal_range(__x);
  1183.       size_type __n = distance(__p.first, __p.second);
  1184.       erase(__p.first, __p.second);
  1185.       return __n;
  1186.     }
  1187.  
  1188.   template<typename _Key, typename _Val, typename _KoV, 
  1189.            typename _Compare, typename _Alloc>
  1190.     typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type 
  1191.     _Rb_tree<_Key,_Val,_KoV,_Compare,_Alloc>::
  1192.     _M_copy(_Link_type __x, _Link_type __p)
  1193.     {
  1194.       // Structural copy.  __x and __p must be non-null.
  1195.       _Link_type __top = _M_clone_node(__x);
  1196.       __top->_M_parent = __p;
  1197.       
  1198.       try 
  1199.     {
  1200.       if (__x->_M_right)
  1201.         __top->_M_right = _M_copy(_S_right(__x), __top);
  1202.       __p = __top;
  1203.       __x = _S_left(__x);
  1204.       
  1205.       while (__x != 0) 
  1206.         {
  1207.           _Link_type __y = _M_clone_node(__x);
  1208.           __p->_M_left = __y;
  1209.           __y->_M_parent = __p;
  1210.           if (__x->_M_right)
  1211.         __y->_M_right = _M_copy(_S_right(__x), __y);
  1212.           __p = __y;
  1213.           __x = _S_left(__x);
  1214.         }
  1215.     }
  1216.       catch(...)
  1217.     {
  1218.       _M_erase(__top);
  1219.       __throw_exception_again; 
  1220.     }
  1221.       return __top;
  1222.     }
  1223.  
  1224.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1225.            typename _Compare, typename _Alloc>
  1226.     void 
  1227.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::_M_erase(_Link_type __x)
  1228.     {
  1229.       // Erase without rebalancing.
  1230.       while (__x != 0) 
  1231.     {
  1232.       _M_erase(_S_right(__x));
  1233.       _Link_type __y = _S_left(__x);
  1234.       destroy_node(__x);
  1235.       __x = __y;
  1236.     }
  1237.     }
  1238.  
  1239.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1240.            typename _Compare, typename _Alloc>
  1241.     void 
  1242.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1243.     erase(iterator __first, iterator __last)
  1244.     {
  1245.       if (__first == begin() && __last == end())
  1246.     clear();
  1247.       else
  1248.     while (__first != __last) erase(__first++);
  1249.     }
  1250.  
  1251.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1252.            typename _Compare, typename _Alloc>
  1253.     void 
  1254.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1255.     erase(const _Key* __first, const _Key* __last) 
  1256.     { 
  1257.       while (__first != __last) 
  1258.     erase(*__first++); 
  1259.     }
  1260.  
  1261.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1262.            typename _Compare, typename _Alloc>
  1263.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator 
  1264.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k)
  1265.     {
  1266.       _Link_type __y = _M_header;  // Last node which is not less than __k. 
  1267.       _Link_type __x = _M_root();  // Current node. 
  1268.       
  1269.       while (__x != 0) 
  1270.     if (!_M_key_compare(_S_key(__x), __k))
  1271.       __y = __x, __x = _S_left(__x);
  1272.     else
  1273.       __x = _S_right(__x);
  1274.       
  1275.       iterator __j = iterator(__y);   
  1276.       return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ? 
  1277.     end() : __j;
  1278.     }
  1279.   
  1280.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1281.            typename _Compare, typename _Alloc>
  1282.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator 
  1283.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1284.     find(const _Key& __k) const
  1285.     {
  1286.       _Link_type __y = _M_header; // Last node which is not less than __k. 
  1287.       _Link_type __x = _M_root(); // Current node. 
  1288.  
  1289.      while (__x != 0) 
  1290.        {
  1291.      if (!_M_key_compare(_S_key(__x), __k))
  1292.        __y = __x, __x = _S_left(__x);
  1293.      else
  1294.        __x = _S_right(__x);
  1295.        } 
  1296.      const_iterator __j = const_iterator(__y);   
  1297.      return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
  1298.        end() : __j;
  1299.     }
  1300.  
  1301.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1302.            typename _Compare, typename _Alloc>
  1303.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type 
  1304.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1305.     count(const _Key& __k) const
  1306.     {
  1307.       pair<const_iterator, const_iterator> __p = equal_range(__k);
  1308.       size_type __n = distance(__p.first, __p.second);
  1309.       return __n;
  1310.     }
  1311.  
  1312.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1313.            typename _Compare, typename _Alloc>
  1314.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator 
  1315.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1316.     lower_bound(const _Key& __k)
  1317.     {
  1318.       _Link_type __y = _M_header; /* Last node which is not less than __k. */
  1319.       _Link_type __x = _M_root(); /* Current node. */
  1320.       
  1321.       while (__x != 0) 
  1322.     if (!_M_key_compare(_S_key(__x), __k))
  1323.       __y = __x, __x = _S_left(__x);
  1324.     else
  1325.       __x = _S_right(__x);
  1326.       
  1327.       return iterator(__y);
  1328.     }
  1329.  
  1330.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1331.            typename _Compare, typename _Alloc>
  1332.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator 
  1333.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1334.     lower_bound(const _Key& __k) const
  1335.     {
  1336.       _Link_type __y = _M_header; /* Last node which is not less than __k. */
  1337.       _Link_type __x = _M_root(); /* Current node. */
  1338.       
  1339.       while (__x != 0) 
  1340.     if (!_M_key_compare(_S_key(__x), __k))
  1341.       __y = __x, __x = _S_left(__x);
  1342.     else
  1343.       __x = _S_right(__x);
  1344.       
  1345.       return const_iterator(__y);
  1346.     }
  1347.  
  1348.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1349.            typename _Compare, typename _Alloc>
  1350.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator 
  1351.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1352.     upper_bound(const _Key& __k)
  1353.     {
  1354.       _Link_type __y = _M_header; /* Last node which is greater than __k. */
  1355.       _Link_type __x = _M_root(); /* Current node. */
  1356.       
  1357.       while (__x != 0) 
  1358.     if (_M_key_compare(__k, _S_key(__x)))
  1359.       __y = __x, __x = _S_left(__x);
  1360.     else
  1361.       __x = _S_right(__x);
  1362.       
  1363.       return iterator(__y);
  1364.     }
  1365.  
  1366.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1367.            typename _Compare, typename _Alloc>
  1368.     typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator 
  1369.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1370.     upper_bound(const _Key& __k) const
  1371.     {
  1372.       _Link_type __y = _M_header; /* Last node which is greater than __k. */
  1373.       _Link_type __x = _M_root(); /* Current node. */
  1374.       
  1375.       while (__x != 0) 
  1376.     if (_M_key_compare(__k, _S_key(__x)))
  1377.       __y = __x, __x = _S_left(__x);
  1378.     else
  1379.       __x = _S_right(__x);
  1380.       
  1381.       return const_iterator(__y);
  1382.     }
  1383.  
  1384.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1385.            typename _Compare, typename _Alloc>
  1386.     inline 
  1387.     pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator,
  1388.                                    typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator>
  1389.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
  1390.     equal_range(const _Key& __k)
  1391.     { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
  1392.  
  1393.   template<typename _Key, typename _Val, typename _KoV, 
  1394.            typename _Compare, typename _Alloc>
  1395.   inline 
  1396.   pair<typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator,
  1397.                                 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
  1398.   _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>
  1399.   ::equal_range(const _Key& __k) const
  1400.   {
  1401.     return pair<const_iterator,const_iterator>(lower_bound(__k),
  1402.                            upper_bound(__k));
  1403.   }
  1404.  
  1405.   inline int
  1406.   __black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root)
  1407.   {
  1408.     if (__node == 0)
  1409.       return 0;
  1410.     int __sum = 0;
  1411.     do 
  1412.       {
  1413.     if (__node->_M_color == _M_black) 
  1414.       ++__sum;
  1415.     if (__node == __root) 
  1416.       break;
  1417.     __node = __node->_M_parent;
  1418.       } 
  1419.     while (1);
  1420.     return __sum;
  1421.   }
  1422.  
  1423.   template<typename _Key, typename _Val, typename _KeyOfValue, 
  1424.            typename _Compare, typename _Alloc>
  1425.     bool 
  1426.     _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
  1427.     {
  1428.     if (_M_node_count == 0 || begin() == end())
  1429.       return _M_node_count == 0 && begin() == end() &&
  1430.     _M_header->_M_left == _M_header && _M_header->_M_right == _M_header;
  1431.   
  1432.     int __len = __black_count(_M_leftmost(), _M_root());
  1433.     for (const_iterator __it = begin(); __it != end(); ++__it) 
  1434.       {
  1435.     _Link_type __x = (_Link_type) __it._M_node;
  1436.     _Link_type __L = _S_left(__x);
  1437.     _Link_type __R = _S_right(__x);
  1438.     
  1439.     if (__x->_M_color == _M_red)
  1440.       if ((__L && __L->_M_color == _M_red) 
  1441.           || (__R && __R->_M_color == _M_red))
  1442.         return false;
  1443.     
  1444.     if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
  1445.       return false;
  1446.     if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
  1447.       return false;
  1448.  
  1449.     if (!__L && !__R && __black_count(__x, _M_root()) != __len)
  1450.       return false;
  1451.       }
  1452.     
  1453.     if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
  1454.       return false;
  1455.     if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
  1456.       return false;
  1457.     return true;
  1458.     }
  1459. } // namespace std 
  1460.  
  1461. #endif 
  1462.