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