Import stripped gcc-4.0.1 sources.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / bits / stl_tree.h
blobc51456360753dbc4ba16fefa59d7d6fc21129496
1 // RB tree implementation -*- C++ -*-
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.
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.
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.
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.
32 * Copyright (c) 1996,1997
33 * Silicon Graphics Computer Systems, Inc.
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.
44 * Copyright (c) 1994
45 * Hewlett-Packard Company
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.
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.
63 #ifndef _TREE_H
64 #define _TREE_H 1
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>
72 namespace std
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
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.
90 enum _Rb_tree_color { _S_red = false, _S_black = true };
92 struct _Rb_tree_node_base
94 typedef _Rb_tree_node_base* _Base_ptr;
95 typedef const _Rb_tree_node_base* _Const_Base_ptr;
97 _Rb_tree_color _M_color;
98 _Base_ptr _M_parent;
99 _Base_ptr _M_left;
100 _Base_ptr _M_right;
102 static _Base_ptr
103 _S_minimum(_Base_ptr __x)
105 while (__x->_M_left != 0) __x = __x->_M_left;
106 return __x;
109 static _Const_Base_ptr
110 _S_minimum(_Const_Base_ptr __x)
112 while (__x->_M_left != 0) __x = __x->_M_left;
113 return __x;
116 static _Base_ptr
117 _S_maximum(_Base_ptr __x)
119 while (__x->_M_right != 0) __x = __x->_M_right;
120 return __x;
123 static _Const_Base_ptr
124 _S_maximum(_Const_Base_ptr __x)
126 while (__x->_M_right != 0) __x = __x->_M_right;
127 return __x;
131 template<typename _Val>
132 struct _Rb_tree_node : public _Rb_tree_node_base
134 typedef _Rb_tree_node<_Val>* _Link_type;
135 _Val _M_value_field;
138 _Rb_tree_node_base*
139 _Rb_tree_increment(_Rb_tree_node_base* __x);
141 const _Rb_tree_node_base*
142 _Rb_tree_increment(const _Rb_tree_node_base* __x);
144 _Rb_tree_node_base*
145 _Rb_tree_decrement(_Rb_tree_node_base* __x);
147 const _Rb_tree_node_base*
148 _Rb_tree_decrement(const _Rb_tree_node_base* __x);
150 template<typename _Tp>
151 struct _Rb_tree_iterator
153 typedef _Tp value_type;
154 typedef _Tp& reference;
155 typedef _Tp* pointer;
157 typedef bidirectional_iterator_tag iterator_category;
158 typedef ptrdiff_t difference_type;
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;
164 _Rb_tree_iterator()
165 : _M_node() { }
167 _Rb_tree_iterator(_Link_type __x)
168 : _M_node(__x) { }
170 reference
171 operator*() const
172 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
174 pointer
175 operator->() const
176 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
178 _Self&
179 operator++()
181 _M_node = _Rb_tree_increment(_M_node);
182 return *this;
185 _Self
186 operator++(int)
188 _Self __tmp = *this;
189 _M_node = _Rb_tree_increment(_M_node);
190 return __tmp;
193 _Self&
194 operator--()
196 _M_node = _Rb_tree_decrement(_M_node);
197 return *this;
200 _Self
201 operator--(int)
203 _Self __tmp = *this;
204 _M_node = _Rb_tree_decrement(_M_node);
205 return __tmp;
208 bool
209 operator==(const _Self& __x) const
210 { return _M_node == __x._M_node; }
212 bool
213 operator!=(const _Self& __x) const
214 { return _M_node != __x._M_node; }
216 _Base_ptr _M_node;
219 template<typename _Tp>
220 struct _Rb_tree_const_iterator
222 typedef _Tp value_type;
223 typedef const _Tp& reference;
224 typedef const _Tp* pointer;
226 typedef _Rb_tree_iterator<_Tp> iterator;
228 typedef bidirectional_iterator_tag iterator_category;
229 typedef ptrdiff_t difference_type;
231 typedef _Rb_tree_const_iterator<_Tp> _Self;
232 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
233 typedef const _Rb_tree_node<_Tp>* _Link_type;
235 _Rb_tree_const_iterator()
236 : _M_node() { }
238 _Rb_tree_const_iterator(_Link_type __x)
239 : _M_node(__x) { }
241 _Rb_tree_const_iterator(const iterator& __it)
242 : _M_node(__it._M_node) { }
244 reference
245 operator*() const
246 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
248 pointer
249 operator->() const
250 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
252 _Self&
253 operator++()
255 _M_node = _Rb_tree_increment(_M_node);
256 return *this;
259 _Self
260 operator++(int)
262 _Self __tmp = *this;
263 _M_node = _Rb_tree_increment(_M_node);
264 return __tmp;
267 _Self&
268 operator--()
270 _M_node = _Rb_tree_decrement(_M_node);
271 return *this;
274 _Self
275 operator--(int)
277 _Self __tmp = *this;
278 _M_node = _Rb_tree_decrement(_M_node);
279 return __tmp;
282 bool
283 operator==(const _Self& __x) const
284 { return _M_node == __x._M_node; }
286 bool
287 operator!=(const _Self& __x) const
288 { return _M_node != __x._M_node; }
290 _Base_ptr _M_node;
293 template<typename _Val>
294 inline bool
295 operator==(const _Rb_tree_iterator<_Val>& __x,
296 const _Rb_tree_const_iterator<_Val>& __y)
297 { return __x._M_node == __y._M_node; }
299 template<typename _Val>
300 inline bool
301 operator!=(const _Rb_tree_iterator<_Val>& __x,
302 const _Rb_tree_const_iterator<_Val>& __y)
303 { return __x._M_node != __y._M_node; }
305 void
306 _Rb_tree_rotate_left(_Rb_tree_node_base* const __x,
307 _Rb_tree_node_base*& __root);
309 void
310 _Rb_tree_rotate_right(_Rb_tree_node_base* const __x,
311 _Rb_tree_node_base*& __root);
313 void
314 _Rb_tree_insert_and_rebalance(const bool __insert_left,
315 _Rb_tree_node_base* __x,
316 _Rb_tree_node_base* __p,
317 _Rb_tree_node_base& __header);
319 _Rb_tree_node_base*
320 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
321 _Rb_tree_node_base& __header);
324 template<typename _Key, typename _Val, typename _KeyOfValue,
325 typename _Compare, typename _Alloc = allocator<_Val> >
326 class _Rb_tree
328 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
329 _Node_allocator;
331 protected:
332 typedef _Rb_tree_node_base* _Base_ptr;
333 typedef const _Rb_tree_node_base* _Const_Base_ptr;
334 typedef _Rb_tree_node<_Val> _Rb_tree_node;
336 public:
337 typedef _Key key_type;
338 typedef _Val value_type;
339 typedef value_type* pointer;
340 typedef const value_type* const_pointer;
341 typedef value_type& reference;
342 typedef const value_type& const_reference;
343 typedef _Rb_tree_node* _Link_type;
344 typedef const _Rb_tree_node* _Const_Link_type;
345 typedef size_t size_type;
346 typedef ptrdiff_t difference_type;
347 typedef _Alloc allocator_type;
349 allocator_type
350 get_allocator() const
351 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
353 protected:
354 _Rb_tree_node*
355 _M_get_node()
356 { return _M_impl._Node_allocator::allocate(1); }
358 void
359 _M_put_node(_Rb_tree_node* __p)
360 { _M_impl._Node_allocator::deallocate(__p, 1); }
362 _Link_type
363 _M_create_node(const value_type& __x)
365 _Link_type __tmp = _M_get_node();
367 { get_allocator().construct(&__tmp->_M_value_field, __x); }
368 catch(...)
370 _M_put_node(__tmp);
371 __throw_exception_again;
373 return __tmp;
376 _Link_type
377 _M_clone_node(_Const_Link_type __x)
379 _Link_type __tmp = _M_create_node(__x->_M_value_field);
380 __tmp->_M_color = __x->_M_color;
381 __tmp->_M_left = 0;
382 __tmp->_M_right = 0;
383 return __tmp;
386 void
387 destroy_node(_Link_type __p)
389 get_allocator().destroy(&__p->_M_value_field);
390 _M_put_node(__p);
393 protected:
394 template<typename _Key_compare,
395 bool _Is_pod_comparator = std::__is_pod<_Key_compare>::__value>
396 struct _Rb_tree_impl : public _Node_allocator
398 _Key_compare _M_key_compare;
399 _Rb_tree_node_base _M_header;
400 size_type _M_node_count; // Keeps track of size of tree.
402 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
403 const _Key_compare& __comp = _Key_compare())
404 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
406 this->_M_header._M_color = _S_red;
407 this->_M_header._M_parent = 0;
408 this->_M_header._M_left = &this->_M_header;
409 this->_M_header._M_right = &this->_M_header;
413 // Specialization for _Comparison types that are not capable of
414 // being base classes / super classes.
415 template<typename _Key_compare>
416 struct _Rb_tree_impl<_Key_compare, true> : public _Node_allocator
418 _Key_compare _M_key_compare;
419 _Rb_tree_node_base _M_header;
420 size_type _M_node_count; // Keeps track of size of tree.
422 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
423 const _Key_compare& __comp = _Key_compare())
424 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
426 this->_M_header._M_color = _S_red;
427 this->_M_header._M_parent = 0;
428 this->_M_header._M_left = &this->_M_header;
429 this->_M_header._M_right = &this->_M_header;
433 _Rb_tree_impl<_Compare> _M_impl;
435 protected:
436 _Base_ptr&
437 _M_root()
438 { return this->_M_impl._M_header._M_parent; }
440 _Const_Base_ptr
441 _M_root() const
442 { return this->_M_impl._M_header._M_parent; }
444 _Base_ptr&
445 _M_leftmost()
446 { return this->_M_impl._M_header._M_left; }
448 _Const_Base_ptr
449 _M_leftmost() const
450 { return this->_M_impl._M_header._M_left; }
452 _Base_ptr&
453 _M_rightmost()
454 { return this->_M_impl._M_header._M_right; }
456 _Const_Base_ptr
457 _M_rightmost() const
458 { return this->_M_impl._M_header._M_right; }
460 _Link_type
461 _M_begin()
462 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
464 _Const_Link_type
465 _M_begin() const
467 return static_cast<_Const_Link_type>
468 (this->_M_impl._M_header._M_parent);
471 _Link_type
472 _M_end()
473 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
475 _Const_Link_type
476 _M_end() const
477 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
479 static const_reference
480 _S_value(_Const_Link_type __x)
481 { return __x->_M_value_field; }
483 static const _Key&
484 _S_key(_Const_Link_type __x)
485 { return _KeyOfValue()(_S_value(__x)); }
487 static _Link_type
488 _S_left(_Base_ptr __x)
489 { return static_cast<_Link_type>(__x->_M_left); }
491 static _Const_Link_type
492 _S_left(_Const_Base_ptr __x)
493 { return static_cast<_Const_Link_type>(__x->_M_left); }
495 static _Link_type
496 _S_right(_Base_ptr __x)
497 { return static_cast<_Link_type>(__x->_M_right); }
499 static _Const_Link_type
500 _S_right(_Const_Base_ptr __x)
501 { return static_cast<_Const_Link_type>(__x->_M_right); }
503 static const_reference
504 _S_value(_Const_Base_ptr __x)
505 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
507 static const _Key&
508 _S_key(_Const_Base_ptr __x)
509 { return _KeyOfValue()(_S_value(__x)); }
511 static _Base_ptr
512 _S_minimum(_Base_ptr __x)
513 { return _Rb_tree_node_base::_S_minimum(__x); }
515 static _Const_Base_ptr
516 _S_minimum(_Const_Base_ptr __x)
517 { return _Rb_tree_node_base::_S_minimum(__x); }
519 static _Base_ptr
520 _S_maximum(_Base_ptr __x)
521 { return _Rb_tree_node_base::_S_maximum(__x); }
523 static _Const_Base_ptr
524 _S_maximum(_Const_Base_ptr __x)
525 { return _Rb_tree_node_base::_S_maximum(__x); }
527 public:
528 typedef _Rb_tree_iterator<value_type> iterator;
529 typedef _Rb_tree_const_iterator<value_type> const_iterator;
531 typedef std::reverse_iterator<iterator> reverse_iterator;
532 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
534 private:
535 iterator
536 _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
538 _Link_type
539 _M_copy(_Const_Link_type __x, _Link_type __p);
541 void
542 _M_erase(_Link_type __x);
544 public:
545 // allocation/deallocation
546 _Rb_tree()
549 _Rb_tree(const _Compare& __comp)
550 : _M_impl(allocator_type(), __comp)
553 _Rb_tree(const _Compare& __comp, const allocator_type& __a)
554 : _M_impl(__a, __comp)
557 _Rb_tree(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
558 : _M_impl(__x.get_allocator(), __x._M_impl._M_key_compare)
560 if (__x._M_root() != 0)
562 _M_root() = _M_copy(__x._M_begin(), _M_end());
563 _M_leftmost() = _S_minimum(_M_root());
564 _M_rightmost() = _S_maximum(_M_root());
565 _M_impl._M_node_count = __x._M_impl._M_node_count;
569 ~_Rb_tree()
570 { _M_erase(_M_begin()); }
572 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
573 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x);
575 // Accessors.
576 _Compare
577 key_comp() const
578 { return _M_impl._M_key_compare; }
580 iterator
581 begin()
582 { return static_cast<_Link_type>(this->_M_impl._M_header._M_left); }
584 const_iterator
585 begin() const
587 return static_cast<_Const_Link_type>
588 (this->_M_impl._M_header._M_left);
591 iterator
592 end()
593 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
595 const_iterator
596 end() const
597 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
599 reverse_iterator
600 rbegin()
601 { return reverse_iterator(end()); }
603 const_reverse_iterator
604 rbegin() const
605 { return const_reverse_iterator(end()); }
607 reverse_iterator
608 rend()
609 { return reverse_iterator(begin()); }
611 const_reverse_iterator
612 rend() const
613 { return const_reverse_iterator(begin()); }
615 bool
616 empty() const
617 { return _M_impl._M_node_count == 0; }
619 size_type
620 size() const
621 { return _M_impl._M_node_count; }
623 size_type
624 max_size() const
625 { return size_type(-1); }
627 void
628 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t);
630 // Insert/erase.
631 pair<iterator,bool>
632 insert_unique(const value_type& __x);
634 iterator
635 insert_equal(const value_type& __x);
637 iterator
638 insert_unique(iterator __position, const value_type& __x);
640 iterator
641 insert_equal(iterator __position, const value_type& __x);
643 template<typename _InputIterator>
644 void
645 insert_unique(_InputIterator __first, _InputIterator __last);
647 template<typename _InputIterator>
648 void
649 insert_equal(_InputIterator __first, _InputIterator __last);
651 void
652 erase(iterator __position);
654 size_type
655 erase(const key_type& __x);
657 void
658 erase(iterator __first, iterator __last);
660 void
661 erase(const key_type* __first, const key_type* __last);
663 void
664 clear()
666 _M_erase(_M_begin());
667 _M_leftmost() = _M_end();
668 _M_root() = 0;
669 _M_rightmost() = _M_end();
670 _M_impl._M_node_count = 0;
673 // Set operations.
674 iterator
675 find(const key_type& __x);
677 const_iterator
678 find(const key_type& __x) const;
680 size_type
681 count(const key_type& __x) const;
683 iterator
684 lower_bound(const key_type& __x);
686 const_iterator
687 lower_bound(const key_type& __x) const;
689 iterator
690 upper_bound(const key_type& __x);
692 const_iterator
693 upper_bound(const key_type& __x) const;
695 pair<iterator,iterator>
696 equal_range(const key_type& __x);
698 pair<const_iterator, const_iterator>
699 equal_range(const key_type& __x) const;
701 // Debugging.
702 bool
703 __rb_verify() const;
706 template<typename _Key, typename _Val, typename _KeyOfValue,
707 typename _Compare, typename _Alloc>
708 inline bool
709 operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
710 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
712 return __x.size() == __y.size()
713 && std::equal(__x.begin(), __x.end(), __y.begin());
716 template<typename _Key, typename _Val, typename _KeyOfValue,
717 typename _Compare, typename _Alloc>
718 inline bool
719 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
720 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
722 return std::lexicographical_compare(__x.begin(), __x.end(),
723 __y.begin(), __y.end());
726 template<typename _Key, typename _Val, typename _KeyOfValue,
727 typename _Compare, typename _Alloc>
728 inline bool
729 operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
730 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
731 { return !(__x == __y); }
733 template<typename _Key, typename _Val, typename _KeyOfValue,
734 typename _Compare, typename _Alloc>
735 inline bool
736 operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
737 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
738 { return __y < __x; }
740 template<typename _Key, typename _Val, typename _KeyOfValue,
741 typename _Compare, typename _Alloc>
742 inline bool
743 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
744 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
745 { return !(__y < __x); }
747 template<typename _Key, typename _Val, typename _KeyOfValue,
748 typename _Compare, typename _Alloc>
749 inline bool
750 operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
751 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
752 { return !(__x < __y); }
754 template<typename _Key, typename _Val, typename _KeyOfValue,
755 typename _Compare, typename _Alloc>
756 inline void
757 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
758 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
759 { __x.swap(__y); }
761 template<typename _Key, typename _Val, typename _KeyOfValue,
762 typename _Compare, typename _Alloc>
763 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
764 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
765 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
767 if (this != &__x)
769 // Note that _Key may be a constant type.
770 clear();
771 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
772 if (__x._M_root() != 0)
774 _M_root() = _M_copy(__x._M_begin(), _M_end());
775 _M_leftmost() = _S_minimum(_M_root());
776 _M_rightmost() = _S_maximum(_M_root());
777 _M_impl._M_node_count = __x._M_impl._M_node_count;
780 return *this;
783 template<typename _Key, typename _Val, typename _KeyOfValue,
784 typename _Compare, typename _Alloc>
785 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
786 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
787 _M_insert(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
789 bool __insert_left = (__x != 0 || __p == _M_end()
790 || _M_impl._M_key_compare(_KeyOfValue()(__v),
791 _S_key(__p)));
793 _Link_type __z = _M_create_node(__v);
795 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
796 this->_M_impl._M_header);
797 ++_M_impl._M_node_count;
798 return iterator(__z);
801 template<typename _Key, typename _Val, typename _KeyOfValue,
802 typename _Compare, typename _Alloc>
803 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
804 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
805 insert_equal(const _Val& __v)
807 _Link_type __x = _M_begin();
808 _Link_type __y = _M_end();
809 while (__x != 0)
811 __y = __x;
812 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
813 _S_left(__x) : _S_right(__x);
815 return _M_insert(__x, __y, __v);
818 template<typename _Key, typename _Val, typename _KeyOfValue,
819 typename _Compare, typename _Alloc>
820 void
821 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
822 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
824 if (_M_root() == 0)
826 if (__t._M_root() != 0)
828 _M_root() = __t._M_root();
829 _M_leftmost() = __t._M_leftmost();
830 _M_rightmost() = __t._M_rightmost();
831 _M_root()->_M_parent = _M_end();
833 __t._M_root() = 0;
834 __t._M_leftmost() = __t._M_end();
835 __t._M_rightmost() = __t._M_end();
838 else if (__t._M_root() == 0)
840 __t._M_root() = _M_root();
841 __t._M_leftmost() = _M_leftmost();
842 __t._M_rightmost() = _M_rightmost();
843 __t._M_root()->_M_parent = __t._M_end();
845 _M_root() = 0;
846 _M_leftmost() = _M_end();
847 _M_rightmost() = _M_end();
849 else
851 std::swap(_M_root(),__t._M_root());
852 std::swap(_M_leftmost(),__t._M_leftmost());
853 std::swap(_M_rightmost(),__t._M_rightmost());
855 _M_root()->_M_parent = _M_end();
856 __t._M_root()->_M_parent = __t._M_end();
858 // No need to swap header's color as it does not change.
859 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
860 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
863 template<typename _Key, typename _Val, typename _KeyOfValue,
864 typename _Compare, typename _Alloc>
865 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
866 _Compare, _Alloc>::iterator, bool>
867 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
868 insert_unique(const _Val& __v)
870 _Link_type __x = _M_begin();
871 _Link_type __y = _M_end();
872 bool __comp = true;
873 while (__x != 0)
875 __y = __x;
876 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
877 __x = __comp ? _S_left(__x) : _S_right(__x);
879 iterator __j = iterator(__y);
880 if (__comp)
881 if (__j == begin())
882 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
883 else
884 --__j;
885 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
886 return pair<iterator, bool>(_M_insert(__x, __y, __v), true);
887 return pair<iterator, bool>(__j, false);
890 template<typename _Key, typename _Val, typename _KeyOfValue,
891 typename _Compare, typename _Alloc>
892 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
893 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
894 insert_unique(iterator __position, const _Val& __v)
896 if (__position._M_node == _M_end()
897 || __position._M_node == _M_rightmost())
899 if (size() > 0
900 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
901 _KeyOfValue()(__v)))
902 return _M_insert(0, _M_rightmost(), __v);
903 else
904 return insert_unique(__v).first;
906 else
908 iterator __after = __position;
909 ++__after;
910 if (_M_impl._M_key_compare(_S_key(__position._M_node),
911 _KeyOfValue()(__v))
912 && _M_impl._M_key_compare(_KeyOfValue()(__v),
913 _S_key(__after._M_node)))
915 if (_S_right(__position._M_node) == 0)
916 return _M_insert(0, __position._M_node, __v);
917 else
918 return _M_insert(__after._M_node, __after._M_node, __v);
919 // First argument just needs to be non-null.
921 else
922 return insert_unique(__v).first;
926 template<typename _Key, typename _Val, typename _KeyOfValue,
927 typename _Compare, typename _Alloc>
928 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
929 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
930 insert_equal(iterator __position, const _Val& __v)
932 if (__position._M_node == _M_end()
933 || __position._M_node == _M_rightmost())
935 if (size() > 0
936 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
937 _S_key(_M_rightmost())))
938 return _M_insert(0, _M_rightmost(), __v);
939 else
940 return insert_equal(__v);
942 else
944 iterator __after = __position;
945 ++__after;
946 if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
947 _S_key(__position._M_node))
948 && !_M_impl._M_key_compare(_S_key(__after._M_node),
949 _KeyOfValue()(__v)))
951 if (_S_right(__position._M_node) == 0)
952 return _M_insert(0, __position._M_node, __v);
953 else
954 return _M_insert(__after._M_node, __after._M_node, __v);
955 // First argument just needs to be non-null.
957 else
958 return insert_equal(__v);
962 template<typename _Key, typename _Val, typename _KoV,
963 typename _Cmp, typename _Alloc>
964 template<class _II>
965 void
966 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
967 insert_equal(_II __first, _II __last)
969 for (; __first != __last; ++__first)
970 insert_equal(end(), *__first);
973 template<typename _Key, typename _Val, typename _KoV,
974 typename _Cmp, typename _Alloc>
975 template<class _II>
976 void
977 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
978 insert_unique(_II __first, _II __last)
980 for (; __first != __last; ++__first)
981 insert_unique(end(), *__first);
984 template<typename _Key, typename _Val, typename _KeyOfValue,
985 typename _Compare, typename _Alloc>
986 inline void
987 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
988 erase(iterator __position)
990 _Link_type __y =
991 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
992 (__position._M_node,
993 this->_M_impl._M_header));
994 destroy_node(__y);
995 --_M_impl._M_node_count;
998 template<typename _Key, typename _Val, typename _KeyOfValue,
999 typename _Compare, typename _Alloc>
1000 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1001 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1002 erase(const _Key& __x)
1004 pair<iterator,iterator> __p = equal_range(__x);
1005 size_type __n = std::distance(__p.first, __p.second);
1006 erase(__p.first, __p.second);
1007 return __n;
1010 template<typename _Key, typename _Val, typename _KoV,
1011 typename _Compare, typename _Alloc>
1012 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1013 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1014 _M_copy(_Const_Link_type __x, _Link_type __p)
1016 // Structural copy. __x and __p must be non-null.
1017 _Link_type __top = _M_clone_node(__x);
1018 __top->_M_parent = __p;
1022 if (__x->_M_right)
1023 __top->_M_right = _M_copy(_S_right(__x), __top);
1024 __p = __top;
1025 __x = _S_left(__x);
1027 while (__x != 0)
1029 _Link_type __y = _M_clone_node(__x);
1030 __p->_M_left = __y;
1031 __y->_M_parent = __p;
1032 if (__x->_M_right)
1033 __y->_M_right = _M_copy(_S_right(__x), __y);
1034 __p = __y;
1035 __x = _S_left(__x);
1038 catch(...)
1040 _M_erase(__top);
1041 __throw_exception_again;
1043 return __top;
1046 template<typename _Key, typename _Val, typename _KeyOfValue,
1047 typename _Compare, typename _Alloc>
1048 void
1049 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1050 _M_erase(_Link_type __x)
1052 // Erase without rebalancing.
1053 while (__x != 0)
1055 _M_erase(_S_right(__x));
1056 _Link_type __y = _S_left(__x);
1057 destroy_node(__x);
1058 __x = __y;
1062 template<typename _Key, typename _Val, typename _KeyOfValue,
1063 typename _Compare, typename _Alloc>
1064 void
1065 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1066 erase(iterator __first, iterator __last)
1068 if (__first == begin() && __last == end())
1069 clear();
1070 else
1071 while (__first != __last)
1072 erase(__first++);
1075 template<typename _Key, typename _Val, typename _KeyOfValue,
1076 typename _Compare, typename _Alloc>
1077 void
1078 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1079 erase(const _Key* __first, const _Key* __last)
1081 while (__first != __last)
1082 erase(*__first++);
1085 template<typename _Key, typename _Val, typename _KeyOfValue,
1086 typename _Compare, typename _Alloc>
1087 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1088 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1089 find(const _Key& __k)
1091 _Link_type __x = _M_begin(); // Current node.
1092 _Link_type __y = _M_end(); // Last node which is not less than __k.
1094 while (__x != 0)
1095 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1096 __y = __x, __x = _S_left(__x);
1097 else
1098 __x = _S_right(__x);
1100 iterator __j = iterator(__y);
1101 return (__j == end()
1102 || _M_impl._M_key_compare(__k,
1103 _S_key(__j._M_node))) ? end() : __j;
1106 template<typename _Key, typename _Val, typename _KeyOfValue,
1107 typename _Compare, typename _Alloc>
1108 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1109 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1110 find(const _Key& __k) const
1112 _Const_Link_type __x = _M_begin(); // Current node.
1113 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1115 while (__x != 0)
1117 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1118 __y = __x, __x = _S_left(__x);
1119 else
1120 __x = _S_right(__x);
1122 const_iterator __j = const_iterator(__y);
1123 return (__j == end()
1124 || _M_impl._M_key_compare(__k,
1125 _S_key(__j._M_node))) ? end() : __j;
1128 template<typename _Key, typename _Val, typename _KeyOfValue,
1129 typename _Compare, typename _Alloc>
1130 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1131 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1132 count(const _Key& __k) const
1134 pair<const_iterator, const_iterator> __p = equal_range(__k);
1135 const size_type __n = std::distance(__p.first, __p.second);
1136 return __n;
1139 template<typename _Key, typename _Val, typename _KeyOfValue,
1140 typename _Compare, typename _Alloc>
1141 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1142 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1143 lower_bound(const _Key& __k)
1145 _Link_type __x = _M_begin(); // Current node.
1146 _Link_type __y = _M_end(); // Last node which is not less than __k.
1148 while (__x != 0)
1149 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1150 __y = __x, __x = _S_left(__x);
1151 else
1152 __x = _S_right(__x);
1154 return iterator(__y);
1157 template<typename _Key, typename _Val, typename _KeyOfValue,
1158 typename _Compare, typename _Alloc>
1159 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1160 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1161 lower_bound(const _Key& __k) const
1163 _Const_Link_type __x = _M_begin(); // Current node.
1164 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1166 while (__x != 0)
1167 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1168 __y = __x, __x = _S_left(__x);
1169 else
1170 __x = _S_right(__x);
1172 return const_iterator(__y);
1175 template<typename _Key, typename _Val, typename _KeyOfValue,
1176 typename _Compare, typename _Alloc>
1177 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1178 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1179 upper_bound(const _Key& __k)
1181 _Link_type __x = _M_begin(); // Current node.
1182 _Link_type __y = _M_end(); // Last node which is greater than __k.
1184 while (__x != 0)
1185 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1186 __y = __x, __x = _S_left(__x);
1187 else
1188 __x = _S_right(__x);
1190 return iterator(__y);
1193 template<typename _Key, typename _Val, typename _KeyOfValue,
1194 typename _Compare, typename _Alloc>
1195 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1196 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1197 upper_bound(const _Key& __k) const
1199 _Const_Link_type __x = _M_begin(); // Current node.
1200 _Const_Link_type __y = _M_end(); // Last node which is greater than __k.
1202 while (__x != 0)
1203 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1204 __y = __x, __x = _S_left(__x);
1205 else
1206 __x = _S_right(__x);
1208 return const_iterator(__y);
1211 template<typename _Key, typename _Val, typename _KeyOfValue,
1212 typename _Compare, typename _Alloc>
1213 inline
1214 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1215 _Compare, _Alloc>::iterator,
1216 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator>
1217 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1218 equal_range(const _Key& __k)
1219 { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
1221 template<typename _Key, typename _Val, typename _KoV,
1222 typename _Compare, typename _Alloc>
1223 inline
1224 pair<typename _Rb_tree<_Key, _Val, _KoV,
1225 _Compare, _Alloc>::const_iterator,
1226 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
1227 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1228 equal_range(const _Key& __k) const
1229 { return pair<const_iterator, const_iterator>(lower_bound(__k),
1230 upper_bound(__k)); }
1232 unsigned int
1233 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1234 const _Rb_tree_node_base* __root);
1236 template<typename _Key, typename _Val, typename _KeyOfValue,
1237 typename _Compare, typename _Alloc>
1238 bool
1239 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1241 if (_M_impl._M_node_count == 0 || begin() == end())
1242 return _M_impl._M_node_count == 0 && begin() == end()
1243 && this->_M_impl._M_header._M_left == _M_end()
1244 && this->_M_impl._M_header._M_right == _M_end();
1246 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1247 for (const_iterator __it = begin(); __it != end(); ++__it)
1249 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1250 _Const_Link_type __L = _S_left(__x);
1251 _Const_Link_type __R = _S_right(__x);
1253 if (__x->_M_color == _S_red)
1254 if ((__L && __L->_M_color == _S_red)
1255 || (__R && __R->_M_color == _S_red))
1256 return false;
1258 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1259 return false;
1260 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1261 return false;
1263 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1264 return false;
1267 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1268 return false;
1269 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1270 return false;
1271 return true;
1273 } // namespace std
1275 #endif