1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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)
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,
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.
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.
59 * This is an internal header file, included by other library headers.
60 * You should not attempt to use it directly.
68 Red-black tree class, designed for use in implementing STL
69 associative containers (set, multiset, map, and multimap). The
70 insertion and deletion algorithms are based on those in Cormen,
71 Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
74 (1) the header cell is maintained with links not only to the root
75 but also to the leftmost node of the tree, to enable constant time
76 begin(), and to the rightmost node of the tree, to enable linear time
77 performance when used with the generic set algorithms (set_union,
80 (2) when a node being deleted has two children its successor node is
81 relinked into its place, rather than copied, so that the only
82 iterators invalidated are those referring to the deleted node.
86 #include <bits/stl_algobase.h>
87 #include <bits/allocator.h>
88 #include <bits/stl_construct.h>
89 #include <bits/stl_function.h>
93 enum _Rb_tree_color { _S_red = false, _S_black = true };
95 struct _Rb_tree_node_base
97 typedef _Rb_tree_node_base* _Base_ptr;
98 typedef const _Rb_tree_node_base* _Const_Base_ptr;
100 _Rb_tree_color _M_color;
106 _S_minimum(_Base_ptr __x)
108 while (__x->_M_left != 0) __x = __x->_M_left;
112 static _Const_Base_ptr
113 _S_minimum(_Const_Base_ptr __x)
115 while (__x->_M_left != 0) __x = __x->_M_left;
120 _S_maximum(_Base_ptr __x)
122 while (__x->_M_right != 0) __x = __x->_M_right;
126 static _Const_Base_ptr
127 _S_maximum(_Const_Base_ptr __x)
129 while (__x->_M_right != 0) __x = __x->_M_right;
134 template<typename _Val>
135 struct _Rb_tree_node : public _Rb_tree_node_base
137 typedef _Rb_tree_node<_Val>* _Link_type;
142 _Rb_tree_increment(_Rb_tree_node_base* __x);
144 const _Rb_tree_node_base*
145 _Rb_tree_increment(const _Rb_tree_node_base* __x);
148 _Rb_tree_decrement(_Rb_tree_node_base* __x);
150 const _Rb_tree_node_base*
151 _Rb_tree_decrement(const _Rb_tree_node_base* __x);
153 template<typename _Tp>
154 struct _Rb_tree_iterator
156 typedef _Tp value_type;
157 typedef _Tp& reference;
158 typedef _Tp* pointer;
160 typedef bidirectional_iterator_tag iterator_category;
161 typedef ptrdiff_t difference_type;
163 typedef _Rb_tree_iterator<_Tp> _Self;
164 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
165 typedef _Rb_tree_node<_Tp>* _Link_type;
167 _Rb_tree_iterator() {}
169 _Rb_tree_iterator(_Link_type __x)
174 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
178 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
183 _M_node = _Rb_tree_increment(_M_node);
191 _M_node = _Rb_tree_increment(_M_node);
198 _M_node = _Rb_tree_decrement(_M_node);
206 _M_node = _Rb_tree_decrement(_M_node);
211 operator==(const _Self& __x) const
212 { return _M_node == __x._M_node; }
215 operator!=(const _Self& __x) const
216 { return _M_node != __x._M_node; }
221 template<typename _Tp>
222 struct _Rb_tree_const_iterator
224 typedef _Tp value_type;
225 typedef const _Tp& reference;
226 typedef const _Tp* pointer;
228 typedef _Rb_tree_iterator<_Tp> iterator;
230 typedef bidirectional_iterator_tag iterator_category;
231 typedef ptrdiff_t difference_type;
233 typedef _Rb_tree_const_iterator<_Tp> _Self;
234 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
235 typedef const _Rb_tree_node<_Tp>* _Link_type;
237 _Rb_tree_const_iterator() {}
239 _Rb_tree_const_iterator(_Link_type __x)
242 _Rb_tree_const_iterator(const iterator& __it)
243 : _M_node(__it._M_node) {}
247 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
251 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
256 _M_node = _Rb_tree_increment(_M_node);
264 _M_node = _Rb_tree_increment(_M_node);
271 _M_node = _Rb_tree_decrement(_M_node);
279 _M_node = _Rb_tree_decrement(_M_node);
284 operator==(const _Self& __x) const
285 { return _M_node == __x._M_node; }
288 operator!=(const _Self& __x) const
289 { return _M_node != __x._M_node; }
294 template<typename _Val>
296 operator==(const _Rb_tree_iterator<_Val>& __x,
297 const _Rb_tree_const_iterator<_Val>& __y)
298 { return __x._M_node == __y._M_node; }
300 template<typename _Val>
302 operator!=(const _Rb_tree_iterator<_Val>& __x,
303 const _Rb_tree_const_iterator<_Val>& __y)
304 { return __x._M_node != __y._M_node; }
307 _Rb_tree_rotate_left(_Rb_tree_node_base* const __x,
308 _Rb_tree_node_base*& __root);
311 _Rb_tree_rotate_right(_Rb_tree_node_base* const __x,
312 _Rb_tree_node_base*& __root);
315 _Rb_tree_insert_and_rebalance(const bool __insert_left,
316 _Rb_tree_node_base* __x,
317 _Rb_tree_node_base* __p,
318 _Rb_tree_node_base& __header);
321 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
322 _Rb_tree_node_base& __header);
325 template<typename _Key, typename _Val, typename _KeyOfValue,
326 typename _Compare, typename _Alloc = allocator<_Val> >
328 : protected _Alloc::template rebind<_Rb_tree_node<_Val> >::other
330 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
334 typedef _Rb_tree_node_base* _Base_ptr;
335 typedef const _Rb_tree_node_base* _Const_Base_ptr;
336 typedef _Rb_tree_node<_Val> _Rb_tree_node;
339 typedef _Key key_type;
340 typedef _Val value_type;
341 typedef value_type* pointer;
342 typedef const value_type* const_pointer;
343 typedef value_type& reference;
344 typedef const value_type& const_reference;
345 typedef _Rb_tree_node* _Link_type;
346 typedef const _Rb_tree_node* _Const_Link_type;
347 typedef size_t size_type;
348 typedef ptrdiff_t difference_type;
350 typedef _Alloc allocator_type;
351 allocator_type get_allocator() const
352 { return *static_cast<const _Node_allocator*>(this); }
357 { return _Node_allocator::allocate(1); }
360 _M_put_node(_Rb_tree_node* __p)
361 { _Node_allocator::deallocate(__p, 1); }
364 _M_create_node(const value_type& __x)
366 _Link_type __tmp = _M_get_node();
368 { std::_Construct(&__tmp->_M_value_field, __x); }
372 __throw_exception_again;
378 _M_clone_node(_Const_Link_type __x)
380 _Link_type __tmp = _M_create_node(__x->_M_value_field);
381 __tmp->_M_color = __x->_M_color;
388 destroy_node(_Link_type __p)
390 std::_Destroy(&__p->_M_value_field);
395 _Rb_tree_node_base _M_header;
396 size_type _M_node_count; // keeps track of size of tree
397 _Compare _M_key_compare;
402 { return this->_M_header._M_parent; }
406 { return this->_M_header._M_parent; }
410 { return this->_M_header._M_left; }
414 { return this->_M_header._M_left; }
418 { return this->_M_header._M_right; }
422 { return this->_M_header._M_right; }
426 { return static_cast<_Link_type>(this->_M_header._M_parent); }
430 { return static_cast<_Const_Link_type>(this->_M_header._M_parent); }
434 { return static_cast<_Link_type>(&this->_M_header); }
438 { return static_cast<_Const_Link_type>(&this->_M_header); }
440 static const_reference
441 _S_value(_Const_Link_type __x)
442 { return __x->_M_value_field; }
445 _S_key(_Const_Link_type __x)
446 { return _KeyOfValue()(_S_value(__x)); }
449 _S_left(_Base_ptr __x)
450 { return static_cast<_Link_type>(__x->_M_left); }
452 static _Const_Link_type
453 _S_left(_Const_Base_ptr __x)
454 { return static_cast<_Const_Link_type>(__x->_M_left); }
457 _S_right(_Base_ptr __x)
458 { return static_cast<_Link_type>(__x->_M_right); }
460 static _Const_Link_type
461 _S_right(_Const_Base_ptr __x)
462 { return static_cast<_Const_Link_type>(__x->_M_right); }
464 static const_reference
465 _S_value(_Const_Base_ptr __x)
466 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
469 _S_key(_Const_Base_ptr __x)
470 { return _KeyOfValue()(_S_value(__x)); }
473 _S_minimum(_Base_ptr __x)
474 { return _Rb_tree_node_base::_S_minimum(__x); }
476 static _Const_Base_ptr
477 _S_minimum(_Const_Base_ptr __x)
478 { return _Rb_tree_node_base::_S_minimum(__x); }
481 _S_maximum(_Base_ptr __x)
482 { return _Rb_tree_node_base::_S_maximum(__x); }
484 static _Const_Base_ptr
485 _S_maximum(_Const_Base_ptr __x)
486 { return _Rb_tree_node_base::_S_maximum(__x); }
489 typedef _Rb_tree_iterator<value_type> iterator;
490 typedef _Rb_tree_const_iterator<value_type> const_iterator;
492 typedef std::reverse_iterator<iterator> reverse_iterator;
493 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
497 _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
500 _M_copy(_Const_Link_type __x, _Link_type __p);
503 _M_erase(_Link_type __x);
506 // allocation/deallocation
508 : _Node_allocator(allocator_type()),
511 { _M_empty_initialize(); }
513 _Rb_tree(const _Compare& __comp)
514 : _Node_allocator(allocator_type()),
516 _M_key_compare(__comp)
517 { _M_empty_initialize(); }
519 _Rb_tree(const _Compare& __comp, const allocator_type& __a)
520 : _Node_allocator(__a),
522 _M_key_compare(__comp)
523 { _M_empty_initialize(); }
525 _Rb_tree(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x)
526 : _Node_allocator(__x.get_allocator()),
528 _M_key_compare(__x._M_key_compare)
530 if (__x._M_root() == 0)
531 _M_empty_initialize();
534 this->_M_header._M_color = _S_red;
535 _M_root() = _M_copy(__x._M_begin(), _M_end());
536 _M_leftmost() = _S_minimum(_M_root());
537 _M_rightmost() = _S_maximum(_M_root());
539 _M_node_count = __x._M_node_count;
543 { _M_erase(_M_begin()); }
545 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>&
546 operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x);
549 void _M_empty_initialize()
551 // Used to distinguish header from __root, in iterator.operator++.
552 this->_M_header._M_color = _S_red;
554 _M_leftmost() = _M_end();
555 _M_rightmost() = _M_end();
562 { return _M_key_compare; }
566 { return static_cast<_Link_type>(this->_M_header._M_left); }
570 { return static_cast<_Const_Link_type>(this->_M_header._M_left); }
574 { return static_cast<_Link_type>(&this->_M_header); }
578 { return static_cast<_Const_Link_type>(&this->_M_header); }
582 { return reverse_iterator(end()); }
584 const_reverse_iterator
586 { return const_reverse_iterator(end()); }
590 { return reverse_iterator(begin()); }
592 const_reverse_iterator
594 { return const_reverse_iterator(begin()); }
598 { return _M_node_count == 0; }
602 { return _M_node_count; }
606 { return size_type(-1); }
609 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t);
613 insert_unique(const value_type& __x);
616 insert_equal(const value_type& __x);
619 insert_unique(iterator __position, const value_type& __x);
622 insert_equal(iterator __position, const value_type& __x);
624 template<typename _InputIterator>
626 insert_unique(_InputIterator __first, _InputIterator __last);
628 template<typename _InputIterator>
630 insert_equal(_InputIterator __first, _InputIterator __last);
633 erase(iterator __position);
636 erase(const key_type& __x);
639 erase(iterator __first, iterator __last);
642 erase(const key_type* __first, const key_type* __last);
647 _M_erase(_M_begin());
648 _M_leftmost() = _M_end();
650 _M_rightmost() = _M_end();
656 find(const key_type& __x);
659 find(const key_type& __x) const;
662 count(const key_type& __x) const;
665 lower_bound(const key_type& __x);
668 lower_bound(const key_type& __x) const;
671 upper_bound(const key_type& __x);
674 upper_bound(const key_type& __x) const;
676 pair<iterator,iterator>
677 equal_range(const key_type& __x);
679 pair<const_iterator, const_iterator>
680 equal_range(const key_type& __x) const;
687 template<typename _Key, typename _Val, typename _KeyOfValue,
688 typename _Compare, typename _Alloc>
690 operator==(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
691 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
693 return __x.size() == __y.size()
694 && equal(__x.begin(), __x.end(), __y.begin());
697 template<typename _Key, typename _Val, typename _KeyOfValue,
698 typename _Compare, typename _Alloc>
700 operator<(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
701 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
703 return lexicographical_compare(__x.begin(), __x.end(),
704 __y.begin(), __y.end());
707 template<typename _Key, typename _Val, typename _KeyOfValue,
708 typename _Compare, typename _Alloc>
710 operator!=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
711 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
712 { return !(__x == __y); }
714 template<typename _Key, typename _Val, typename _KeyOfValue,
715 typename _Compare, typename _Alloc>
717 operator>(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
718 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
719 { return __y < __x; }
721 template<typename _Key, typename _Val, typename _KeyOfValue,
722 typename _Compare, typename _Alloc>
724 operator<=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
725 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
726 { return !(__y < __x); }
728 template<typename _Key, typename _Val, typename _KeyOfValue,
729 typename _Compare, typename _Alloc>
731 operator>=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
732 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
733 { return !(__x < __y); }
735 template<typename _Key, typename _Val, typename _KeyOfValue,
736 typename _Compare, typename _Alloc>
738 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
739 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
742 template<typename _Key, typename _Val, typename _KeyOfValue,
743 typename _Compare, typename _Alloc>
744 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>&
745 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
746 operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x)
750 // Note that _Key may be a constant type.
752 _M_key_compare = __x._M_key_compare;
753 if (__x._M_root() != 0)
755 _M_root() = _M_copy(__x._M_begin(), _M_end());
756 _M_leftmost() = _S_minimum(_M_root());
757 _M_rightmost() = _S_maximum(_M_root());
758 _M_node_count = __x._M_node_count;
764 template<typename _Key, typename _Val, typename _KeyOfValue,
765 typename _Compare, typename _Alloc>
766 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
767 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
768 _M_insert(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
770 _Link_type __z = _M_create_node(__v);
773 __insert_left = __x != 0 || __p == _M_end()
774 || _M_key_compare(_KeyOfValue()(__v), _S_key(__p));
776 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p, this->_M_header);
778 return iterator(__z);
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 insert_equal(const _Val& __v)
787 _Link_type __x = _M_begin();
788 _Link_type __y = _M_end();
792 __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
793 _S_left(__x) : _S_right(__x);
795 return _M_insert(__x, __y, __v);
798 template<typename _Key, typename _Val, typename _KeyOfValue,
799 typename _Compare, typename _Alloc>
801 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
802 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t)
806 if (__t._M_root() != 0)
808 _M_root() = __t._M_root();
809 _M_leftmost() = __t._M_leftmost();
810 _M_rightmost() = __t._M_rightmost();
811 _M_root()->_M_parent = _M_end();
814 __t._M_leftmost() = __t._M_end();
815 __t._M_rightmost() = __t._M_end();
818 else if (__t._M_root() == 0)
820 __t._M_root() = _M_root();
821 __t._M_leftmost() = _M_leftmost();
822 __t._M_rightmost() = _M_rightmost();
823 __t._M_root()->_M_parent = __t._M_end();
826 _M_leftmost() = _M_end();
827 _M_rightmost() = _M_end();
831 std::swap(_M_root(),__t._M_root());
832 std::swap(_M_leftmost(),__t._M_leftmost());
833 std::swap(_M_rightmost(),__t._M_rightmost());
835 _M_root()->_M_parent = _M_end();
836 __t._M_root()->_M_parent = __t._M_end();
838 // No need to swap header's color as it does not change.
839 std::swap(this->_M_node_count, __t._M_node_count);
840 std::swap(this->_M_key_compare, __t._M_key_compare);
843 template<typename _Key, typename _Val, typename _KeyOfValue,
844 typename _Compare, typename _Alloc>
845 pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator,
847 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
848 insert_unique(const _Val& __v)
850 _Link_type __x = _M_begin();
851 _Link_type __y = _M_end();
856 __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
857 __x = __comp ? _S_left(__x) : _S_right(__x);
859 iterator __j = iterator(__y);
862 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
865 if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
866 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
867 return pair<iterator,bool>(__j, false);
870 template<typename _Key, typename _Val, typename _KeyOfValue,
871 typename _Compare, typename _Alloc>
872 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
873 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
874 insert_unique(iterator __position, const _Val& __v)
876 if (__position._M_node == _M_leftmost())
880 && _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
881 return _M_insert(__position._M_node, __position._M_node, __v);
882 // first argument just needs to be non-null
884 return insert_unique(__v).first;
886 else if (__position._M_node == _M_end())
889 if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
890 return _M_insert(0, _M_rightmost(), __v);
892 return insert_unique(__v).first;
896 iterator __before = __position;
898 if (_M_key_compare(_S_key(__before._M_node), _KeyOfValue()(__v))
899 && _M_key_compare(_KeyOfValue()(__v),_S_key(__position._M_node)))
901 if (_S_right(__before._M_node) == 0)
902 return _M_insert(0, __before._M_node, __v);
904 return _M_insert(__position._M_node, __position._M_node, __v);
905 // first argument just needs to be non-null
908 return insert_unique(__v).first;
912 template<typename _Key, typename _Val, typename _KeyOfValue,
913 typename _Compare, typename _Alloc>
914 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
915 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
916 insert_equal(iterator __position, const _Val& __v)
918 if (__position._M_node == _M_leftmost())
922 && !_M_key_compare(_S_key(__position._M_node),
924 return _M_insert(__position._M_node, __position._M_node, __v);
925 // first argument just needs to be non-null
927 return insert_equal(__v);
929 else if (__position._M_node == _M_end())
932 if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
933 return _M_insert(0, _M_rightmost(), __v);
935 return insert_equal(__v);
939 iterator __before = __position;
941 if (!_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))
942 && !_M_key_compare(_S_key(__position._M_node),
945 if (_S_right(__before._M_node) == 0)
946 return _M_insert(0, __before._M_node, __v);
948 return _M_insert(__position._M_node, __position._M_node, __v);
949 // first argument just needs to be non-null
952 return insert_equal(__v);
956 template<typename _Key, typename _Val, typename _KoV,
957 typename _Cmp, typename _Alloc>
960 _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
961 insert_equal(_II __first, _II __last)
963 for ( ; __first != __last; ++__first)
964 insert_equal(*__first);
967 template<typename _Key, typename _Val, typename _KoV,
968 typename _Cmp, typename _Alloc>
971 _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
972 insert_unique(_II __first, _II __last)
974 for ( ; __first != __last; ++__first)
975 insert_unique(*__first);
978 template<typename _Key, typename _Val, typename _KeyOfValue,
979 typename _Compare, typename _Alloc>
981 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(iterator __position)
984 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase(__position._M_node,
990 template<typename _Key, typename _Val, typename _KeyOfValue,
991 typename _Compare, typename _Alloc>
992 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type
993 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(const _Key& __x)
995 pair<iterator,iterator> __p = equal_range(__x);
996 size_type __n = std::distance(__p.first, __p.second);
997 erase(__p.first, __p.second);
1001 template<typename _Key, typename _Val, typename _KoV,
1002 typename _Compare, typename _Alloc>
1003 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1004 _Rb_tree<_Key,_Val,_KoV,_Compare,_Alloc>::
1005 _M_copy(_Const_Link_type __x, _Link_type __p)
1007 // Structural copy. __x and __p must be non-null.
1008 _Link_type __top = _M_clone_node(__x);
1009 __top->_M_parent = __p;
1014 __top->_M_right = _M_copy(_S_right(__x), __top);
1020 _Link_type __y = _M_clone_node(__x);
1022 __y->_M_parent = __p;
1024 __y->_M_right = _M_copy(_S_right(__x), __y);
1032 __throw_exception_again;
1037 template<typename _Key, typename _Val, typename _KeyOfValue,
1038 typename _Compare, typename _Alloc>
1040 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::_M_erase(_Link_type __x)
1042 // Erase without rebalancing.
1045 _M_erase(_S_right(__x));
1046 _Link_type __y = _S_left(__x);
1052 template<typename _Key, typename _Val, typename _KeyOfValue,
1053 typename _Compare, typename _Alloc>
1055 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1056 erase(iterator __first, iterator __last)
1058 if (__first == begin() && __last == end())
1061 while (__first != __last) erase(__first++);
1064 template<typename _Key, typename _Val, typename _KeyOfValue,
1065 typename _Compare, typename _Alloc>
1067 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1068 erase(const _Key* __first, const _Key* __last)
1070 while (__first != __last)
1074 template<typename _Key, typename _Val, typename _KeyOfValue,
1075 typename _Compare, typename _Alloc>
1076 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1077 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k)
1079 _Link_type __x = _M_begin(); // Current node.
1080 _Link_type __y = _M_end(); // Last node which is not less than __k.
1083 if (!_M_key_compare(_S_key(__x), __k))
1084 __y = __x, __x = _S_left(__x);
1086 __x = _S_right(__x);
1088 iterator __j = iterator(__y);
1089 return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
1093 template<typename _Key, typename _Val, typename _KeyOfValue,
1094 typename _Compare, typename _Alloc>
1095 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1096 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1097 find(const _Key& __k) const
1099 _Const_Link_type __x = _M_begin(); // Current node.
1100 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1104 if (!_M_key_compare(_S_key(__x), __k))
1105 __y = __x, __x = _S_left(__x);
1107 __x = _S_right(__x);
1109 const_iterator __j = const_iterator(__y);
1110 return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
1114 template<typename _Key, typename _Val, typename _KeyOfValue,
1115 typename _Compare, typename _Alloc>
1116 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type
1117 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1118 count(const _Key& __k) const
1120 pair<const_iterator, const_iterator> __p = equal_range(__k);
1121 const size_type __n = std::distance(__p.first, __p.second);
1125 template<typename _Key, typename _Val, typename _KeyOfValue,
1126 typename _Compare, typename _Alloc>
1127 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1128 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1129 lower_bound(const _Key& __k)
1131 _Link_type __x = _M_begin(); // Current node.
1132 _Link_type __y = _M_end(); // Last node which is not less than __k.
1135 if (!_M_key_compare(_S_key(__x), __k))
1136 __y = __x, __x = _S_left(__x);
1138 __x = _S_right(__x);
1140 return iterator(__y);
1143 template<typename _Key, typename _Val, typename _KeyOfValue,
1144 typename _Compare, typename _Alloc>
1145 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1146 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1147 lower_bound(const _Key& __k) const
1149 _Const_Link_type __x = _M_begin(); // Current node.
1150 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1153 if (!_M_key_compare(_S_key(__x), __k))
1154 __y = __x, __x = _S_left(__x);
1156 __x = _S_right(__x);
1158 return const_iterator(__y);
1161 template<typename _Key, typename _Val, typename _KeyOfValue,
1162 typename _Compare, typename _Alloc>
1163 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1164 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1165 upper_bound(const _Key& __k)
1167 _Link_type __x = _M_begin(); // Current node.
1168 _Link_type __y = _M_end(); // Last node which is greater than __k.
1171 if (_M_key_compare(__k, _S_key(__x)))
1172 __y = __x, __x = _S_left(__x);
1174 __x = _S_right(__x);
1176 return iterator(__y);
1179 template<typename _Key, typename _Val, typename _KeyOfValue,
1180 typename _Compare, typename _Alloc>
1181 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1182 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1183 upper_bound(const _Key& __k) const
1185 _Const_Link_type __x = _M_begin(); // Current node.
1186 _Const_Link_type __y = _M_end(); // Last node which is greater than __k.
1189 if (_M_key_compare(__k, _S_key(__x)))
1190 __y = __x, __x = _S_left(__x);
1192 __x = _S_right(__x);
1194 return const_iterator(__y);
1197 template<typename _Key, typename _Val, typename _KeyOfValue,
1198 typename _Compare, typename _Alloc>
1200 pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,
1201 _Compare,_Alloc>::iterator,
1202 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator>
1203 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1204 equal_range(const _Key& __k)
1205 { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
1207 template<typename _Key, typename _Val, typename _KoV,
1208 typename _Compare, typename _Alloc>
1210 pair<typename _Rb_tree<_Key, _Val, _KoV,
1211 _Compare, _Alloc>::const_iterator,
1212 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
1213 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1214 equal_range(const _Key& __k) const
1215 { return pair<const_iterator, const_iterator>(lower_bound(__k),
1216 upper_bound(__k)); }
1219 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1220 const _Rb_tree_node_base* __root);
1222 template<typename _Key, typename _Val, typename _KeyOfValue,
1223 typename _Compare, typename _Alloc>
1225 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1227 if (_M_node_count == 0 || begin() == end())
1228 return _M_node_count == 0 && begin() == end()
1229 && this->_M_header._M_left == _M_end()
1230 && this->_M_header._M_right == _M_end();
1232 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1233 for (const_iterator __it = begin(); __it != end(); ++__it)
1235 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1236 _Const_Link_type __L = _S_left(__x);
1237 _Const_Link_type __R = _S_right(__x);
1239 if (__x->_M_color == _S_red)
1240 if ((__L && __L->_M_color == _S_red)
1241 || (__R && __R->_M_color == _S_red))
1244 if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
1246 if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
1249 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1253 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1255 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))