home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / tree.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  42KB  |  1,247 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef TREE_H
  17. #define TREE_H
  18.  
  19. /*
  20.  
  21. Red-black tree class, designed for use in implementing STL
  22. associative containers (set, multiset, map, and multimap). The
  23. insertion and deletion algorithms are based on those in Cormen,
  24. Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
  25. except that
  26.  
  27. (1) the header cell is maintained with links not only to the root
  28. but also to the leftmost node of the tree, to enable constant time
  29. begin(), and to the rightmost node of the tree, to enable linear time
  30. performance when used with the generic set algorithms (set_union,
  31. etc.);
  32.  
  33. (2) when a node being deleted has two children its successor node is
  34. relinked into its place, rather than copied, so that the only
  35. iterators invalidated are those referring to the deleted node.
  36.  
  37. */
  38.  
  39. #include <algobase.h>
  40. #include <iterator.h>
  41. #include <function.h>
  42. #ifndef __GNUG__
  43. #include <bool.h>
  44. #endif
  45. #include <projectn.h>
  46.  
  47. #ifndef rb_tree 
  48. #define rb_tree rb_tree
  49. #endif
  50.  
  51. enum __rb_color_type {red, black};
  52.  
  53. struct __rb_tree_node_base {
  54.   enum __rb_color_type color_field;
  55.   void* parent_link;
  56.   void* left_link;
  57.   void* right_link;
  58. };
  59.  
  60. extern __rb_tree_node_base __rb_NIL;
  61.  
  62. template <class Key, class Value, class KeyOfValue, class Compare>
  63. class rb_tree {
  64. protected:
  65.     typedef enum __rb_color_type color_type;
  66.     typedef Allocator<void>::pointer void_pointer;
  67.     struct rb_tree_node;
  68.     friend rb_tree_node;
  69.     struct rb_tree_node  : public __rb_tree_node_base {
  70.         Value value_field;
  71.     };
  72. #ifndef __GNUG__
  73.     static Allocator<rb_tree_node> rb_tree_node_allocator;
  74.     static Allocator<Value> value_allocator;
  75. #endif
  76. public:
  77.     typedef Key key_type;
  78.     typedef Value value_type;
  79.     typedef Allocator<Value>::pointer pointer;
  80.     typedef Allocator<Value>::reference reference;
  81.     typedef Allocator<Value>::const_reference const_reference;
  82.     typedef Allocator<rb_tree_node> rb_tree_node_allocator_type;
  83.     typedef Allocator<rb_tree_node>::pointer link_type;
  84.     typedef Allocator<rb_tree_node>::size_type size_type;
  85.     typedef Allocator<rb_tree_node>::difference_type difference_type;
  86. protected:
  87. #ifndef __GNUG__
  88.     size_type buffer_size() {
  89.         return rb_tree_node_allocator.init_page_size();
  90.     }
  91. #endif
  92.     struct rb_tree_node_buffer;
  93.     friend rb_tree_node_buffer;
  94.     struct rb_tree_node_buffer {
  95.         void_pointer next_buffer;
  96.         link_type buffer;
  97.     };
  98. public:
  99.     typedef Allocator<rb_tree_node_buffer> buffer_allocator_type;
  100.     typedef Allocator<rb_tree_node_buffer>::pointer buffer_pointer;     
  101. protected:
  102. #ifdef __GNUG__
  103.     static Allocator<rb_tree_node_buffer> buffer_allocator;
  104.     static buffer_pointer buffer_list;
  105.     static link_type free_list;
  106.     static link_type next_avail;
  107.     static link_type last;
  108.     link_type get_node() { return (link_type) operator new (sizeof (rb_tree_node)); }
  109.     void put_node(link_type p) { operator delete (p); }
  110. #else
  111.     void add_new_buffer() {
  112.         buffer_pointer tmp = buffer_allocator.allocate((size_type)1);
  113.         tmp->buffer = rb_tree_node_allocator.allocate(buffer_size());
  114.         tmp->next_buffer = buffer_list;
  115.         buffer_list = tmp;
  116.         next_avail = buffer_list->buffer;
  117.         last = next_avail + buffer_size();
  118.     }
  119.     static size_type number_of_trees;
  120.     void deallocate_buffers();
  121.     link_type get_node() {
  122.         link_type tmp = free_list;
  123.         return free_list ? 
  124.             (free_list = (link_type)(free_list->right_link), tmp) 
  125.                 : (next_avail == last ? (add_new_buffer(), next_avail++) 
  126.                    : next_avail++);
  127.         // ugly code for inlining - avoids multiple returns
  128.     }
  129.     void put_node(link_type p) {
  130.         p->right_link = free_list;
  131.         free_list = p;
  132.     }
  133. #endif
  134. protected:
  135.     link_type header;  
  136.     link_type& root() { return parent(header); }
  137.     link_type& root() const { return parent(header); }
  138.     link_type& leftmost() { return left(header); }
  139.     link_type& leftmost() const { return left(header); }
  140.     link_type& rightmost() { return right(header); }
  141.     link_type& rightmost() const { return right(header); }
  142.     size_type node_count; // keeps track of size of tree
  143.     bool insert_always;  // controls whether an element already in the
  144.                          // tree is inserted again
  145. //public:
  146.     Compare key_compare;
  147.     static link_type& left(link_type x) { 
  148.         return (link_type&)((*x).left_link);
  149.     }
  150.     static link_type& right(link_type x) {
  151.         return (link_type&)((*x).right_link); 
  152.     }
  153.     static link_type& parent(link_type x) {
  154.         return (link_type&)((*x).parent_link);
  155.     }
  156.     static reference value(link_type x) { return (*x).value_field; }
  157.     static Allocator<Key>::const_reference key(link_type x) {
  158.         return KeyOfValue()(value(x));
  159.     }
  160.     static color_type& color(link_type x) { 
  161.         return (color_type&)(*x).color_field; }
  162.     static link_type minimum(link_type x) {
  163.         while (left(x) != &__rb_NIL)
  164.             x = left(x);
  165.         return x;
  166.     }
  167.     static link_type maximum(link_type x) {
  168.         while (right(x) != &__rb_NIL)
  169.             x = right(x);
  170.         return x;
  171.     }
  172. public:
  173.     class iterator;
  174.     friend iterator;
  175.     class const_iterator;
  176.     friend const_iterator;
  177.     class iterator : public bidirectional_iterator<Value, difference_type> {
  178.     friend class rb_tree<Key, Value, KeyOfValue, Compare>;
  179.     friend class const_iterator;
  180. /*      
  181.     friend bool operator==(const iterator& x, const iterator& y) {
  182.         return x.node == y.node;
  183.     }
  184. */
  185.     protected:
  186.         link_type node;
  187.         iterator(link_type x) : node(x) {}
  188.     public:
  189.         iterator() {}
  190.         bool operator==(const iterator& y) const { return node == y.node; }
  191.         reference operator*() const { return value(node); }
  192.         iterator& operator++() {
  193.             if (right(node) != &__rb_NIL) {
  194.                 node = right(node);
  195.                 while (left(node) != &__rb_NIL)
  196.                     node = left(node);
  197.             } else {
  198.                 link_type y = parent(node);
  199.                 while (node == right(y)) {
  200.                     node = y;
  201.                     y = parent(y);
  202.                 }
  203.                 if (right(node) != y) // necessary because of rightmost 
  204.                     node = y;
  205.             }
  206.             return *this;
  207.         }
  208.         iterator operator++(int) {
  209.             iterator tmp = *this;
  210.             ++*this;
  211.             return tmp;
  212.         }
  213.         iterator& operator--() {
  214.             if (color(node) == red && parent(parent(node)) == node)  
  215.                 // check for header
  216.                 node = right(node);   // return rightmost
  217.             else if (left(node) != &__rb_NIL) {
  218.                 link_type y = left(node);
  219.                 while (right(y) != &__rb_NIL)
  220.                     y = right(y);
  221.                 node = y;
  222.             } else {
  223.                 link_type y = parent(node);
  224.                 while (node == left(y)) {
  225.                     node = y;
  226.                     y = parent(y);
  227.                 }
  228.                 node = y;
  229.             }
  230.             return *this;
  231.         }
  232.         iterator operator--(int) {
  233.             iterator tmp = *this;
  234.             --*this;
  235.             return tmp;
  236.         }
  237.     };
  238.     class const_iterator 
  239.         : public bidirectional_iterator<Value,difference_type> {
  240.     friend class rb_tree<Key, Value, KeyOfValue, Compare>;
  241.     friend class iterator;
  242. /*      
  243.     friend bool operator==(const const_iterator& x, const const_iterator& y) {
  244.         return x.node == y.node;
  245.     }
  246. */
  247.     protected:
  248.         link_type node;
  249.         const_iterator(link_type x) : node(x) {}
  250.     public:
  251.         const_iterator() {}
  252.         const_iterator(const iterator& x) : node(x.node) {}
  253.         bool operator==(const const_iterator& y) const { 
  254.             return node == y.node; 
  255.         }
  256.         bool operator!=(const const_iterator& y) const { 
  257.             return node != y.node; 
  258.         }
  259.         const_reference operator*() const { return value(node); }
  260.         const_iterator& operator++() {
  261.             if (right(node) != &__rb_NIL) {
  262.                 node = right(node);
  263.                 while (left(node) != &__rb_NIL)
  264.                     node = left(node);
  265.             } else {
  266.                 link_type y = parent(node);
  267.                 while (node == right(y)) {
  268.                     node = y;
  269.                     y = parent(y);
  270.                 }
  271.                 if (right(node) != y) // necessary because of rightmost 
  272.                     node = y;
  273.             }
  274.             return *this;
  275.         }
  276.         const_iterator operator++(int) {
  277.             const_iterator tmp = *this;
  278.             ++*this;
  279.             return tmp;
  280.         }
  281.         const_iterator& operator--() {
  282.             if (color(node) == red && parent(parent(node)) == node)  
  283.                 // check for header
  284.                 node = right(node);   // return rightmost
  285.             else if (left(node) != &__rb_NIL) {
  286.                 link_type y = left(node);
  287.                 while (right(y) != &__rb_NIL)
  288.                     y = right(y);
  289.                 node = y;
  290.             } else {
  291.                 link_type y = parent(node);
  292.                 while (node == left(y)) {
  293.                     node = y;
  294.                     y = parent(y);
  295.                 }
  296.                 node = y;
  297.             }
  298.             return *this;
  299.         }
  300.         const_iterator operator--(int) {
  301.             const_iterator tmp = *this;
  302.             --*this;
  303.             return tmp;
  304.         }
  305.     };
  306.     typedef reverse_bidirectional_iterator<iterator, value_type, reference,
  307.                                            difference_type>
  308.         reverse_iterator; 
  309.     typedef reverse_bidirectional_iterator<const_iterator, value_type,
  310.                                            const_reference, difference_type>
  311.     const_reverse_iterator;
  312. private:
  313. #ifdef __GNUC__
  314.      rb_tree_iterator<Key, Value, KeyOfValue, Compare> __insert(void* x, void* y, const value_type& v);
  315.     link_type __copy(link_type x, link_type p) {
  316.         return (link_type) __copy_hack (x, p);
  317.     }
  318. private:
  319.     void * __copy_hack (void *, void *);
  320. public:
  321.     void __erase(void* x);
  322. #else
  323.     iterator __insert(link_type x, link_type y, const value_type& v);
  324.     link_type __copy(link_type x, link_type p);
  325.     void __erase(link_type x);
  326. #endif
  327.     void init() {
  328. #ifndef __GNUG__
  329.         ++number_of_trees;
  330. #endif
  331.         header = get_node();
  332.         color(header) = red;  // used to distinguish header from root,
  333.                               // in iterator.operator++
  334.         header->parent_link = &__rb_NIL;
  335.         leftmost() = header;
  336.         rightmost() = header;
  337.     }
  338. public:
  339.     
  340. // allocation/deallocation
  341.     
  342.     rb_tree(const Compare& comp = Compare(), bool always = true) 
  343.            : node_count(0), insert_always(always), key_compare(comp) { 
  344.         init();
  345.     }
  346.     rb_tree(const value_type* first, const value_type* last, 
  347.             const Compare& comp = Compare(), bool always = true)
  348.           : node_count(0), insert_always(always), key_compare(comp) { 
  349.         init();
  350.         insert(first, last);
  351.     }
  352.     rb_tree(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  353.             bool always = true) : node_count(x.node_count), 
  354.                  insert_always(always), key_compare(x.key_compare) { 
  355. #ifndef __GNUG__
  356.         ++number_of_trees;
  357. #endif
  358.         header = get_node();
  359.         color(header) = red;
  360.         root() = __copy(x.root(), header);
  361.         if (root() == &__rb_NIL) {
  362.             leftmost() = header;
  363.             rightmost() = header;
  364.         } else {
  365.         leftmost() = minimum(root());
  366.             rightmost() = maximum(root());
  367.         }
  368.     }
  369.     ~rb_tree() {
  370.         erase(begin(), end());
  371.         put_node(header);
  372. #ifndef __GNUG__
  373.         if (--number_of_trees == 0) {
  374.             deallocate_buffers();
  375.             free_list = 0;    
  376.             next_avail = 0;
  377.             last = 0;
  378.         }
  379. #endif
  380.     }
  381.     rb_tree<Key, Value, KeyOfValue, Compare>& 
  382.         operator=(const rb_tree<Key, Value, KeyOfValue, Compare>& x);
  383.     
  384. // accessors:
  385.  
  386.     Compare key_comp() const { return key_compare; }
  387.     iterator begin() { return leftmost(); }
  388.     const_iterator begin() const { return leftmost(); }
  389.     iterator end() { return header; }
  390.     const_iterator end() const { return header; }
  391.     reverse_iterator rbegin() { return reverse_iterator(end()); }
  392.     const_reverse_iterator rbegin() const { 
  393.         return const_reverse_iterator(end()); 
  394.     }
  395.     reverse_iterator rend() { return reverse_iterator(begin()); }
  396.     const_reverse_iterator rend() const { 
  397.         return const_reverse_iterator(begin());
  398.     } 
  399.     bool empty() const { return node_count == 0; }
  400.     size_type size() const { return node_count; }
  401. #ifndef __GNUG__
  402.     size_type max_size() const { 
  403.         return rb_tree_node_allocator.max_size(); 
  404.     }
  405. #else
  406.     size_type max_size() const { 
  407.         return rb_tree_node_allocator_type::max_size(); 
  408.     }
  409. #endif
  410.     void swap(rb_tree<Key, Value, KeyOfValue, Compare>& t) {
  411.         ::swap(header, t.header);
  412.         ::swap(node_count, t.node_count);
  413.         ::swap(insert_always, t.insert_always);
  414.         ::swap(key_compare, t.key_compare);
  415.     }
  416.     
  417. // insert/erase
  418.  
  419.     typedef  pair<iterator, bool> pair_iterator_bool; 
  420.     // typedef done to get around compiler bug
  421. #ifdef __GNUG__
  422.     pair_iterator_bool insert(const value_type& x) {
  423.     return insert_hack(x);
  424.     }
  425. private:
  426.     rb_tree_pair_iterator_bool<Key, Value, KeyOfValue, Compare>
  427.     insert_hack(const Value& v);
  428. public:
  429.     iterator insert(iterator position, const value_type& x) {
  430.         return insert_hack(position, x);
  431.     }
  432. private:
  433.     rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  434.     insert_hack(rb_tree_iterator<Key, Value, KeyOfValue, Compare> posn,
  435.                               const Value& v);
  436. public:
  437.     void insert(iterator first, iterator last) {
  438.         while (first != last) insert(*first++);
  439.     }
  440.     void insert(const value_type* first, const value_type* last){
  441.     while (first != last) insert(*first++);
  442.     }
  443.     void erase(iterator position) {
  444.     erase_hack(position);
  445.     }
  446. private:
  447.     void erase_hack(rb_tree_iterator<Key, Value, KeyOfValue, Compare> position);
  448. public:
  449.     size_type erase(const key_type& x);
  450.     void erase(iterator first, iterator last) {
  451.     while (first != last) erase(first++);
  452.     }
  453. #else
  454.     pair_iterator_bool insert(const value_type& x);
  455.     iterator insert(iterator position, const value_type& x);
  456.     void insert(iterator first, iterator last);
  457.     void insert(const value_type* first, const value_type* last);
  458.     void erase(iterator position);
  459.     size_type erase(const key_type& x);
  460.     void erase(iterator first, iterator last);
  461. #endif
  462.     void erase(const key_type* first, const key_type* last);
  463.  
  464. // set operations:
  465.  
  466. #ifdef __GNUG__
  467.     iterator find(const key_type& x) {
  468.     return find_hack(x);
  469.     }
  470.     const_iterator find(const key_type& x) const {
  471.     return find_hack(x);
  472.     }
  473. private:
  474.     rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  475.         find_hack(const key_type& x);
  476.     rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  477.     find_hack(const Key& k) const;
  478. public:
  479.     
  480.     size_type count(const key_type& x) const;
  481.     iterator lower_bound(const key_type& x) {
  482.     return lower_bound_hack(x);
  483.     }
  484.     const_iterator lower_bound(const key_type& x) const {
  485.     return lower_bound_hack(x);
  486.     }
  487.     iterator upper_bound(const key_type& x) {
  488.     return upper_bound_hack(x);
  489.     }
  490.     const_iterator upper_bound(const key_type& x) const {
  491.     return upper_bound_hack(x);
  492.     }
  493. private:
  494.     rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  495.         lower_bound_hack(const key_type& x);
  496.     rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  497.     lower_bound_hack(const Key& k) const;
  498.     rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  499.         upper_bound_hack(const key_type& x);
  500.     rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  501.     upper_bound_hack(const Key& k) const;
  502. public:
  503.     typedef  pair<iterator, iterator> pair_iterator_iterator; 
  504.     // typedef done to get around compiler bug
  505.     pair_iterator_iterator equal_range(const key_type& x) {
  506.     return pair_iterator_iterator(lower_bound(x), upper_bound(x));
  507.     }
  508.     typedef  pair<const_iterator, const_iterator> pair_citerator_citerator;
  509.     
  510.     // typedef done to get around compiler bug
  511.     pair_citerator_citerator equal_range(const key_type& x) const {
  512.     return pair_citerator_citerator(lower_bound(x), upper_bound(x));
  513.     }
  514.     inline void rotate_left(link_type x) {
  515.     link_type y = right(x);
  516.     right(x) = left(y);
  517.     if (left(y) != &__rb_NIL)
  518.         parent(left(y)) = x;
  519.     parent(y) = parent(x);
  520.     if (x == root())
  521.         root() = y;
  522.     else if (x == left(parent(x)))
  523.         left(parent(x)) = y;
  524.     else
  525.         right(parent(x)) = y;
  526.     left(y) = x;
  527.     parent(x) = y;
  528.     }
  529.  
  530.     inline void rotate_right(link_type x) {
  531.     link_type y = left(x);
  532.     left(x) = right(y);
  533.     if (right(y) != &__rb_NIL)
  534.         parent(right(y)) = x;
  535.     parent(y) = parent(x);
  536.     if (x == root())
  537.         root() = y;
  538.     else if (x == right(parent(x)))
  539.         right(parent(x)) = y;
  540.     else
  541.         left(parent(x)) = y;
  542.     right(y) = x;
  543.     parent(x) = y;
  544.     }
  545.     friend bidirectional_iterator_tag iterator_category(iterator) {
  546.     return bidirectional_iterator_tag();
  547.     }
  548.     friend bidirectional_iterator_tag iterator_category(const_iterator) {
  549.     return bidirectional_iterator_tag();
  550.     }
  551. #else
  552.     iterator find(const key_type& x);
  553.     const_iterator find(const key_type& x) const;
  554.     size_type count(const key_type& x) const;
  555.     iterator lower_bound(const key_type& x);
  556.     const_iterator lower_bound(const key_type& x) const;
  557.     iterator upper_bound(const key_type& x);
  558.     const_iterator upper_bound(const key_type& x) const;
  559.     typedef  pair<iterator, iterator> pair_iterator_iterator; 
  560.     // typedef done to get around compiler bug
  561.     pair_iterator_iterator equal_range(const key_type& x);
  562.     typedef  pair<const_iterator, const_iterator> pair_citerator_citerator; 
  563.     // typedef done to get around compiler bug
  564.     pair_citerator_citerator equal_range(const key_type& x) const;
  565.     inline void rotate_left(link_type x);
  566.     inline void rotate_right(link_type x);
  567. #endif
  568. };
  569.  
  570. #ifndef __GNUG__
  571. template <class Key, class Value, class KeyOfValue, class Compare>
  572. rb_tree<Key, Value, KeyOfValue, Compare>::buffer_pointer 
  573.         rb_tree<Key, Value, KeyOfValue, Compare>::buffer_list = 0;
  574.  
  575. template <class Key, class Value, class KeyOfValue, class Compare>
  576. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  577.         rb_tree<Key, Value, KeyOfValue, Compare>::free_list = 0;
  578.  
  579. template <class Key, class Value, class KeyOfValue, class Compare>
  580. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  581.         rb_tree<Key, Value, KeyOfValue, Compare>::next_avail = 0;
  582.  
  583. template <class Key, class Value, class KeyOfValue, class Compare>
  584. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  585.         rb_tree<Key, Value, KeyOfValue, Compare>::last = 0;
  586.  
  587. template <class Key, class Value, class KeyOfValue, class Compare>
  588. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  589.         rb_tree<Key, Value, KeyOfValue, Compare>::number_of_trees = 0;
  590.  
  591. template <class Key, class Value, class KeyOfValue, class Compare>
  592. rb_tree<Key, Value, KeyOfValue, Compare>::rb_tree_node_allocator_type 
  593.         rb_tree<Key, Value, KeyOfValue, Compare>::rb_tree_node_allocator;
  594.  
  595. template <class Key, class Value, class KeyOfValue, class Compare>
  596. Allocator<Value> rb_tree<Key, Value, KeyOfValue, Compare>::value_allocator;
  597.  
  598. template <class Key, class Value, class KeyOfValue, class Compare>
  599. rb_tree<Key, Value, KeyOfValue, Compare>::buffer_allocator_type 
  600.         rb_tree<Key, Value, KeyOfValue, Compare>::buffer_allocator;
  601.  
  602. template <class Key, class Value, class KeyOfValue, class Compare>
  603. void rb_tree<Key, Value, KeyOfValue, Compare>::deallocate_buffers() {
  604.     while (buffer_list) {
  605.         buffer_pointer tmp = buffer_list;
  606.         buffer_list = (buffer_pointer)(buffer_list->next_buffer);
  607.         rb_tree_node_allocator.deallocate(tmp->buffer);
  608.         buffer_allocator.deallocate(tmp);
  609.     }
  610. }
  611. #endif
  612.  
  613. #ifdef __GNUC__
  614. template <class Key, class Value, class KeyOfValue, class Compare>
  615. struct rb_tree_iterator {
  616.   rb_tree<Key, Value, KeyOfValue, Compare>::iterator it;
  617.   rb_tree_iterator(rb_tree<Key, Value, KeyOfValue, Compare>::iterator i) : it(i) {}
  618.   operator rb_tree<Key, Value, KeyOfValue, Compare>::iterator() {
  619.     return it;
  620.   }
  621. };
  622.  
  623. template <class Key, class Value, class KeyOfValue, class Compare>
  624. inline Value* value_type(const rb_tree_iterator<Key, Value, KeyOfValue, Compare>&) {
  625.     return (Value*)(0);
  626. }
  627.  
  628. template <class Key, class Value, class KeyOfValue, class Compare>
  629. struct rb_tree_const_iterator {
  630.     rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator it;
  631.     rb_tree_const_iterator(rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator i) : it(i) {}
  632.     operator rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator() {
  633.     return it;
  634.     }
  635. };
  636.  
  637. template <class Key, class Value, class KeyOfValue, class Compare>
  638. inline Value* value_type(const rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>&) {
  639.     return (Value*)(0);
  640. }
  641.  
  642. template <class Key, class Value, class KeyOfValue, class Compare>
  643. struct rb_tree_pair_iterator_bool {
  644.     rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_bool it;
  645.     rb_tree_pair_iterator_bool(rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_bool i) : it(i) {}
  646.     operator rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_bool() {
  647.     return it;
  648.     }
  649. };
  650.  
  651. template <class Key, class Value, class KeyOfValue, class Compare>
  652. inline Value* value_type(rb_tree_pair_iterator_bool<Key, Value, KeyOfValue, Compare>&) {
  653.     return (Value*)(0);
  654. }
  655. #endif
  656.  
  657. template <class Key, class Value, class KeyOfValue, class Compare>
  658. inline bool operator==(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  659.                        const rb_tree<Key, Value, KeyOfValue, Compare>& y) {
  660.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  661. }
  662.  
  663. template <class Key, class Value, class KeyOfValue, class Compare>
  664. inline bool operator<(const rb_tree<Key, Value, KeyOfValue, Compare>& x, 
  665.                       const rb_tree<Key, Value, KeyOfValue, Compare>& y) {
  666.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  667. }
  668.  
  669. template <class Key, class Value, class KeyOfValue, class Compare>
  670. rb_tree<Key, Value, KeyOfValue, Compare>& 
  671. rb_tree<Key, Value, KeyOfValue, Compare>::
  672. operator=(const rb_tree<Key, Value, KeyOfValue, Compare>& x) {
  673.     if (this != &x) {
  674.         // can't be done as in list because Key may be a constant type
  675.         erase(begin(), end());
  676.         root() = __copy(x.root(), header);
  677.         if (root() == &__rb_NIL) {
  678.             leftmost() = header;
  679.             rightmost() = header;
  680.         } else {
  681.         leftmost() = minimum(root());
  682.             rightmost() = maximum(root());
  683.         }
  684.         node_count = x.node_count;
  685.     }
  686.     return *this;
  687. }
  688.  
  689. template <class Key, class Value, class KeyOfValue, class Compare>
  690. #ifdef __GNUC__
  691. rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  692. rb_tree<Key, Value, KeyOfValue, Compare>::__insert
  693. (void* xa, void* ya, const Value& v) {
  694.     link_type x = (link_type)xa;
  695.     link_type y = (link_type)ya;
  696. #else
  697. rb_tree<Key, Value, KeyOfValue, Compare>::iterator
  698. rb_tree<Key, Value, KeyOfValue, Compare>::
  699. __insert(link_type x, link_type y, const Value& v) {
  700. #endif
  701.     ++node_count;
  702.     link_type z = get_node();
  703. #ifdef __GNUG__
  704.     construct(&(value(z)), v);
  705. #else
  706.     construct(value_allocator.address(value(z)), v);
  707. #endif
  708.     if (y == header || x != &__rb_NIL || key_compare(KeyOfValue()(v), key(y))) {
  709.         left(y) = z;  // also makes leftmost() = z when y == header
  710.         if (y == header) {
  711.             root() = z;
  712.             rightmost() = z;
  713.         } else if (y == leftmost())
  714.             leftmost() = z;   // maintain leftmost() pointing to minimum node
  715.     } else {
  716.         right(y) = z;
  717.         if (y == rightmost())
  718.             rightmost() = z;   // maintain rightmost() pointing to maximum node
  719.     }
  720.     parent(z) = y;
  721.     z->left_link = &__rb_NIL;
  722.     z->right_link = &__rb_NIL;
  723.     x = z;  // recolor and rebalance the tree
  724.     color(x) = red;
  725.     while (x != root() && color(parent(x)) == red) 
  726.         if (parent(x) == left(parent(parent(x)))) {
  727.             y = right(parent(parent(x)));
  728.             if (color(y) == red) {
  729.                 color(parent(x)) = black;
  730.                 color(y) = black;
  731.                 color(parent(parent(x))) = red;
  732.                 x = parent(parent(x));
  733.             } else {
  734.                 if (x == right(parent(x))) {
  735.                     x = parent(x);
  736.                     rotate_left(x);
  737.                 }
  738.                 color(parent(x)) = black;
  739.                 color(parent(parent(x))) = red;
  740.                 rotate_right(parent(parent(x)));
  741.             }
  742.         } else {
  743.             y = left(parent(parent(x)));
  744.             if (color(y) == red) {
  745.                 color(parent(x)) = black;
  746.                 color(y) = black;
  747.                 color(parent(parent(x))) = red;
  748.                 x = parent(parent(x));
  749.             } else {
  750.                 if (x == left(parent(x))) {
  751.                     x = parent(x);
  752.                     rotate_right(x);
  753.                 }
  754.                 color(parent(x)) = black;
  755.                 color(parent(parent(x))) = red;
  756.                 rotate_left(parent(parent(x)));
  757.             }
  758.         }
  759.     color(root()) = black;
  760.     return iterator(z);
  761. }
  762.  
  763. template <class Key, class Value, class KeyOfValue, class Compare>
  764. #ifdef __GNUC__
  765. rb_tree_pair_iterator_bool<Key, Value, KeyOfValue, Compare>
  766. rb_tree<Key, Value, KeyOfValue, Compare>::insert_hack(const Value& v) {
  767. #else
  768. rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_bool
  769. rb_tree<Key, Value, KeyOfValue, Compare>::insert(const Value& v) {
  770. #endif
  771.     link_type y = header;
  772.     link_type x = root();
  773.     bool comp = true;
  774.     while (x != &__rb_NIL) {
  775.         y = x;
  776.         comp = key_compare(KeyOfValue()(v), key(x));
  777.         x = comp ? left(x) : right(x);
  778.     }
  779.     if (insert_always)
  780.         return pair_iterator_bool(__insert(x, y, v), true);
  781.     iterator j = iterator(y);   
  782.     if (comp)
  783.         if (j == begin())     
  784.             return pair_iterator_bool(__insert(x, y, v), true);
  785.         else
  786.             --j;
  787.     if (key_compare(key(j.node), KeyOfValue()(v)))
  788.         return pair_iterator_bool(__insert(x, y, v), true);
  789.     return pair_iterator_bool(j, false);
  790. }
  791.  
  792. template <class Key, class Value, class KeyOfValue, class Compare>
  793. #ifdef __GNUC__
  794. rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  795. rb_tree<Key, Value, KeyOfValue, Compare>::insert_hack(rb_tree_iterator<Key, Value, KeyOfValue, Compare> posn,
  796.                                                  const Value& v) {
  797.     iterator position = posn;
  798. #else
  799. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  800. rb_tree<Key, Value, KeyOfValue, Compare>::insert(iterator position,
  801.                                                  const Value& v) {
  802. #endif
  803.     if (position == iterator(begin()))
  804.         if (size() > 0 && key_compare(KeyOfValue()(v), key(position.node)))
  805.             return __insert(position.node, position.node, v);
  806.             // first argument just needs to be non-&__rb_NIL 
  807.         else
  808.             return insert(v).first;
  809.     else if (position == iterator(end()))
  810.         if (key_compare(key(rightmost()), KeyOfValue()(v)))
  811.             return __insert(&__rb_NIL, rightmost(), v);
  812.         else
  813.             return insert(v).first;
  814.     else {
  815.         iterator before = --position;
  816.         if (key_compare(key(before.node), KeyOfValue()(v))
  817.             && key_compare(KeyOfValue()(v), key(position.node)))
  818.             if (right(before.node) == &__rb_NIL)
  819.                 return __insert(&__rb_NIL, before.node, v); 
  820.             else
  821.                 return __insert(position.node, position.node, v);
  822.                 // first argument just needs to be non-&__rb_NIL 
  823.         else
  824.             return insert(v).first;
  825.     }
  826. }
  827.  
  828. #ifndef __GNUC__
  829. template <class Key, class Value, class KeyOfValue, class Compare>
  830. void rb_tree<Key, Value, KeyOfValue, Compare>::insert(iterator first, 
  831.                                                       iterator last) {
  832.     while (first != last) insert(*first++);
  833. }
  834.  
  835. template <class Key, class Value, class KeyOfValue, class Compare>
  836. void rb_tree<Key, Value, KeyOfValue, Compare>::insert(const Value* first, 
  837.                                                       const Value* last) {
  838.     while (first != last) insert(*first++);
  839. }
  840. #endif
  841.          
  842. template <class Key, class Value, class KeyOfValue, class Compare>
  843. #ifdef __GNUC__
  844. void rb_tree<Key, Value, KeyOfValue, Compare>::erase_hack(
  845.     rb_tree_iterator<Key, Value, KeyOfValue, Compare> posn) {
  846.     iterator position = posn;
  847. #else
  848. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(iterator position) {
  849. #endif
  850.     link_type z = position.node;
  851.     link_type y = z;
  852.     link_type x;
  853.     if (left(y) == &__rb_NIL)
  854.         x = right(y);
  855.     else
  856.         if (right(y) == &__rb_NIL) 
  857.             x = left(y);
  858.         else {
  859.             y = right(y);
  860.             while (left(y) != &__rb_NIL)
  861.                 y = left(y);
  862.             x = right(y);
  863.         }
  864.     if (y != z) { // relink y in place of z
  865.         parent(left(z)) = y; 
  866.         left(y) = left(z);
  867.         if (y != right(z)) {
  868.             parent(x) = parent(y); // possibly x == &__rb_NIL
  869.             left(parent(y)) = x;   // y must be a left child
  870.             right(y) = right(z);
  871.             parent(right(z)) = y;
  872.         } else
  873.             parent(x) = y;  // needed in case x == &__rb_NIL
  874.         if (root() == z)
  875.             root() = y;
  876.         else if (left(parent(z)) == z)
  877.             left(parent(z)) = y;
  878.         else 
  879.             right(parent(z)) = y;
  880.         parent(y) = parent(z);
  881.         ::swap(color(y), color(z));
  882.         ::swap(y, z);  
  883.                        // y points to node to be actually deleted,
  884.                        // z points to old z's former successor
  885.     } else {  // y == z
  886.         parent(x) = parent(y);   // possibly x == &__rb_NIL
  887.         if (root() == z)
  888.             root() = x;
  889.         else 
  890.             if (left(parent(z)) == z)
  891.                 left(parent(z)) = x;
  892.             else
  893.                 right(parent(z)) = x;
  894.         if (leftmost() == z) 
  895.             if (right(z) == &__rb_NIL)  // left(z) must be &__rb_NIL also
  896.                 leftmost() = parent(z);
  897.                 // makes leftmost() == header if z == root()
  898.         else
  899.             leftmost() = minimum(x);
  900.         if (rightmost() == z)  
  901.             if (left(z) == &__rb_NIL) // right(z) must be &__rb_NIL also
  902.                 rightmost() = parent(z);  
  903.                 // makes rightmost() == header if z == root()
  904.         else  // x == left(z)
  905.             rightmost() = maximum(x);
  906.     }
  907.     if (color(y) != red) { 
  908.         while (x != root() && color(x) == black)
  909.             if (x == left(parent(x))) {
  910.                 link_type w = right(parent(x));
  911.                 if (color(w) == red) {
  912.                     color(w) = black;
  913.                     color(parent(x)) = red;
  914.                     rotate_left(parent(x));
  915.                     w = right(parent(x));
  916.                 }
  917.                 if (color(left(w)) == black && color(right(w)) == black) {
  918.                     color(w) = red;
  919.                     x = parent(x);
  920.                 } else {
  921.                     if (color(right(w)) == black) {
  922.                         color(left(w)) = black;
  923.                         color(w) = red;
  924.                         rotate_right(w);
  925.                         w = right(parent(x));
  926.                     }
  927.                     color(w) = color(parent(x));
  928.                     color(parent(x)) = black;
  929.                     color(right(w)) = black;
  930.                     rotate_left(parent(x));
  931.                     break;
  932.                 }
  933.             } else {  // same as then clause with "right" and "left" exchanged
  934.                 link_type w = left(parent(x));
  935.                 if (color(w) == red) {
  936.                     color(w) = black;
  937.                     color(parent(x)) = red;
  938.                     rotate_right(parent(x));
  939.                     w = left(parent(x));
  940.                 }
  941.                 if (color(right(w)) == black && color(left(w)) == black) {
  942.                     color(w) = red;
  943.                     x = parent(x);
  944.                 } else {
  945.                     if (color(left(w)) == black) {
  946.                         color(right(w)) = black;
  947.                         color(w) = red;
  948.                         rotate_left(w);
  949.                         w = left(parent(x));
  950.                     }
  951.                     color(w) = color(parent(x));
  952.                     color(parent(x)) = black;
  953.                     color(left(w)) = black;
  954.                     rotate_right(parent(x));
  955.                     break;
  956.                 }
  957.             }
  958.         color(x) = black;
  959.     }
  960. #ifdef __GNUG__
  961.     delete y;
  962. #else
  963.     destroy(value_allocator.address(value(y)));
  964.     put_node(y);
  965. #endif
  966.     --node_count;
  967. }
  968.  
  969. template <class Key, class Value, class KeyOfValue, class Compare>
  970. #ifdef __GNUC__
  971. #ifndef __SIZE_TYPE__
  972. #define __SIZE_TYPE__ long unsigned int
  973. #endif
  974. __SIZE_TYPE__
  975. #else
  976. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  977. #endif
  978. rb_tree<Key, Value, KeyOfValue, Compare>::erase(const Key& x) {
  979.     pair_iterator_iterator p = equal_range(x);
  980.     size_type n = 0;
  981.     distance(p.first, p.second, n);
  982.     erase(p.first, p.second);
  983.     return n;
  984. }
  985.  
  986. template <class Key, class Value, class KeyOfValue, class Compare>
  987. #ifdef __GNUG__
  988. void *
  989. rb_tree<Key, Value, KeyOfValue, Compare>::__copy_hack(void* xa, void* pa) {
  990.    link_type x = (link_type)xa;
  991.    link_type p = (link_type)pa;
  992. #else
  993. rb_tree<Key, Value, KeyOfValue, Compare>::link_type 
  994. rb_tree<Key, Value, KeyOfValue, Compare>::__copy(link_type x, link_type p) {
  995. #endif
  996.    // structural copy
  997.    link_type r = x;
  998.    while (x != &__rb_NIL) {
  999.       link_type y = get_node();
  1000.       if (r == x) r = y;  // save for return value
  1001. #ifdef __GNUG__
  1002.       construct(&(value(y)), value(x));
  1003. #else
  1004.       construct(value_allocator.address(value(y)), value(x));
  1005. #endif
  1006.       left(p) = y;
  1007.       parent(y) = p;
  1008.       color(y) = color(x);
  1009.       right(y) = __copy(right(x), y);
  1010.       p = y;
  1011.       x = left(x);
  1012.    }
  1013.    left(p) = (link_type)&__rb_NIL;
  1014.    return r;
  1015. }
  1016.  
  1017. template <class Key, class Value, class KeyOfValue, class Compare>
  1018. #ifdef __GNUG__
  1019. void rb_tree<Key, Value, KeyOfValue, Compare>::__erase(void* xa) {
  1020.     link_type x = (link_type)xa;
  1021. #else
  1022. void rb_tree<Key, Value, KeyOfValue, Compare>::__erase(link_type x) {
  1023. #endif
  1024.     // erase without rebalancing
  1025.     while (x != &__rb_NIL) {
  1026.        __erase(right(x));
  1027.        link_type y = left(x);
  1028. #ifdef __GNUG__
  1029.        delete x;
  1030. #else
  1031.        destroy(value_allocator.address(value(x)));
  1032.        put_node(x);
  1033. #endif
  1034.        x = y;
  1035.     }
  1036. }
  1037.  
  1038. #ifndef __GNUC__
  1039. template <class Key, class Value, class KeyOfValue, class Compare>
  1040. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(iterator first, 
  1041.                                                      iterator last) {
  1042.     if (first == begin() && last == end() && node_count != 0) {
  1043.         __erase(root());
  1044.         leftmost() = header;
  1045.         root() = NIL;
  1046.         rightmost() = header;
  1047.         node_count = 0;
  1048.     } else
  1049.         while (first != last) erase(first++);
  1050. }
  1051. #endif
  1052.  
  1053. template <class Key, class Value, class KeyOfValue, class Compare>
  1054. void rb_tree<Key, Value, KeyOfValue, Compare>::erase(const Key* first, 
  1055.                                                      const Key* last) {
  1056.     while (first != last) erase(*first++);
  1057. }
  1058.  
  1059. template <class Key, class Value, class KeyOfValue, class Compare>
  1060. #ifdef __GNUC__
  1061. rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  1062. rb_tree<Key, Value, KeyOfValue, Compare>::find_hack(const Key& k) {
  1063. #else
  1064. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  1065. rb_tree<Key, Value, KeyOfValue, Compare>::find(const Key& k) {
  1066. #endif
  1067.     link_type y = header;
  1068.     link_type x = root();
  1069.     bool comp = false;
  1070.     while (x != &__rb_NIL) {
  1071.         y = x;
  1072.         comp = key_compare(key(x), k);
  1073.         x = comp ? right(x) : left(x);
  1074.     }
  1075.     iterator j = iterator(y);   
  1076.     if (comp) ++j;
  1077.     return (j == end() || key_compare(k, key(j.node))) ? end() : j;
  1078. }
  1079.  
  1080. template <class Key, class Value, class KeyOfValue, class Compare>
  1081. #ifdef __GNUC__
  1082. rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  1083. rb_tree<Key, Value, KeyOfValue, Compare>::find_hack(const Key& k) const {
  1084. #else
  1085. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  1086. rb_tree<Key, Value, KeyOfValue, Compare>::find(const Key& k) const {
  1087. #endif
  1088.     link_type y = header;
  1089.     link_type x = root();
  1090.     bool comp = false;
  1091.     while (x != &__rb_NIL) {
  1092.         y = x;
  1093.         comp = key_compare(key(x), k);
  1094.         x = comp ? right(x) : left(x);
  1095.     }
  1096.     const_iterator j = const_iterator(y);   
  1097.     if (comp) ++j;
  1098.     return (j == end() || key_compare(k, key(j.node))) ? end() : j;
  1099. }
  1100.  
  1101. template <class Key, class Value, class KeyOfValue, class Compare>
  1102. #ifdef __GNUG__
  1103. __SIZE_TYPE__
  1104. #else
  1105. rb_tree<Key, Value, KeyOfValue, Compare>::size_type 
  1106. #endif
  1107. rb_tree<Key, Value, KeyOfValue, Compare>::count(const Key& k) const {
  1108.     pair<const_iterator, const_iterator> p = equal_range(k);
  1109.     size_type n = 0;
  1110.     distance(p.first, p.second, n);
  1111.     return n;
  1112. }
  1113.  
  1114. template <class Key, class Value, class KeyOfValue, class Compare>
  1115. #ifdef __GNUC__
  1116. rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  1117. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound_hack(const Key& k) {
  1118. #else
  1119. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  1120. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound(const Key& k) {
  1121. #endif
  1122.     link_type y = header;
  1123.     link_type x = root();
  1124.     bool comp = false;
  1125.     while (x != &__rb_NIL) {
  1126.         y = x;
  1127.         comp = key_compare(key(x), k);
  1128.         x = comp ? right(x) : left(x);
  1129.     }
  1130.     iterator j = iterator(y);   
  1131.     return comp ? ++j : j;
  1132. }
  1133.  
  1134. template <class Key, class Value, class KeyOfValue, class Compare>
  1135. #ifdef __GNUC__
  1136. rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  1137. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound_hack(const Key& k) const {
  1138. #else
  1139. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  1140. rb_tree<Key, Value, KeyOfValue, Compare>::lower_bound(const Key& k) const {
  1141. #endif
  1142.     link_type y = header;
  1143.     link_type x = root();
  1144.     bool comp = false;
  1145.     while (x != &__rb_NIL) {
  1146.         y = x;
  1147.         comp = key_compare(key(x), k);
  1148.         x = comp ? right(x) : left(x);
  1149.     }
  1150.     const_iterator j = const_iterator(y);   
  1151.     return comp ? ++j : j;
  1152. }
  1153.  
  1154. template <class Key, class Value, class KeyOfValue, class Compare>
  1155. #ifdef __GNUC__
  1156. rb_tree_iterator<Key, Value, KeyOfValue, Compare>
  1157. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound_hack(const Key& k) {
  1158. #else
  1159. rb_tree<Key, Value, KeyOfValue, Compare>::iterator 
  1160. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound(const Key& k) {
  1161. #endif
  1162.     link_type y = header;
  1163.     link_type x = root();
  1164.     bool comp = true;
  1165.     while (x != &__rb_NIL) {
  1166.         y = x;
  1167.         comp = key_compare(k, key(x));
  1168.         x = comp ? left(x) : right(x);
  1169.     }
  1170.     iterator j = iterator(y);   
  1171.     return comp ? j : ++j;
  1172. }
  1173.  
  1174. template <class Key, class Value, class KeyOfValue, class Compare>
  1175. #ifdef __GNUC__
  1176. rb_tree_const_iterator<Key, Value, KeyOfValue, Compare>
  1177. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound_hack(const Key& k) const {
  1178. #else
  1179. rb_tree<Key, Value, KeyOfValue, Compare>::const_iterator 
  1180. rb_tree<Key, Value, KeyOfValue, Compare>::upper_bound(const Key& k) const {
  1181. #endif
  1182.     link_type y = header;
  1183.     link_type x = root();
  1184.     bool comp = true;
  1185.     while (x != &__rb_NIL) {
  1186.         y = x;
  1187.         comp = key_compare(k, key(x));
  1188.         x = comp ? left(x) : right(x);
  1189.     }
  1190.     const_iterator j = const_iterator(y);   
  1191.     return comp ? j : ++j;
  1192. }
  1193.  
  1194.  
  1195. #ifndef __GNUC__
  1196. template <class Key, class Value, class KeyOfValue, class Compare>
  1197. rb_tree<Key, Value, KeyOfValue, Compare>::pair_iterator_iterator 
  1198. rb_tree<Key, Value, KeyOfValue, Compare>::equal_range(const Key& k) {
  1199.     return pair_iterator_iterator(lower_bound(k), upper_bound(k));
  1200. }
  1201.  
  1202. template <class Key, class Value, class KeyOfValue, class Compare>
  1203. rb_tree<Key, Value, KeyOfValue, Compare>::pair_citerator_citerator 
  1204. rb_tree<Key, Value, KeyOfValue, Compare>::equal_range(const Key& k) const {
  1205.     return pair_citerator_citerator(lower_bound(k), upper_bound(k));
  1206. }
  1207.  
  1208. template <class Key, class Value, class KeyOfValue, class Compare>
  1209. inline void 
  1210. rb_tree<Key, Value, KeyOfValue, Compare>::rotate_left(link_type x) {
  1211.     link_type y = right(x);
  1212.     right(x) = left(y);
  1213.     if (left(y) != &__rb_NIL)
  1214.         parent(left(y)) = x;
  1215.     parent(y) = parent(x);
  1216.     if (x == root())
  1217.         root() = y;
  1218.     else if (x == left(parent(x)))
  1219.         left(parent(x)) = y;
  1220.     else
  1221.         right(parent(x)) = y;
  1222.     left(y) = x;
  1223.     parent(x) = y;
  1224. }
  1225.  
  1226. template <class Key, class Value, class KeyOfValue, class Compare>
  1227. inline void 
  1228. rb_tree<Key, Value, KeyOfValue, Compare>::rotate_right(link_type x) {
  1229.     link_type y = left(x);
  1230.     left(x) = right(y);
  1231.     if (right(y) != &__rb_NIL)
  1232.         parent(right(y)) = x;
  1233.     parent(y) = parent(x);
  1234.     if (x == root())
  1235.         root() = y;
  1236.     else if (x == right(parent(x)))
  1237.         right(parent(x)) = y;
  1238.     else
  1239.         left(parent(x)) = y;
  1240.     right(y) = x;
  1241.     parent(x) = y;
  1242. }
  1243. #endif
  1244.  
  1245. #endif
  1246.  
  1247.