OSDN Git Service

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