1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Copyright (c) 1996,1997
34 * Silicon Graphics Computer Systems, Inc.
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Silicon Graphics makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Hewlett-Packard Company
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Hewlett-Packard Company makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
60 * This is an internal header file, included by other library headers.
61 * You should not attempt to use it directly.
67 #pragma GCC system_header
69 #include <bits/stl_algobase.h>
70 #include <bits/allocator.h>
71 #include <bits/stl_construct.h>
72 #include <bits/stl_function.h>
73 #include <bits/cpp_type_traits.h>
75 _GLIBCXX_BEGIN_NAMESPACE(std)
77 // Red-black tree class, designed for use in implementing STL
78 // associative containers (set, multiset, map, and multimap). The
79 // insertion and deletion algorithms are based on those in Cormen,
80 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
83 // (1) the header cell is maintained with links not only to the root
84 // but also to the leftmost node of the tree, to enable constant
85 // time begin(), and to the rightmost node of the tree, to enable
86 // linear time performance when used with the generic set algorithms
89 // (2) when a node being deleted has two children its successor node
90 // is relinked into its place, rather than copied, so that the only
91 // iterators invalidated are those referring to the deleted node.
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;
171 _Rb_tree_iterator(_Link_type __x)
176 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
180 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
185 _M_node = _Rb_tree_increment(_M_node);
193 _M_node = _Rb_tree_increment(_M_node);
200 _M_node = _Rb_tree_decrement(_M_node);
208 _M_node = _Rb_tree_decrement(_M_node);
213 operator==(const _Self& __x) const
214 { return _M_node == __x._M_node; }
217 operator!=(const _Self& __x) const
218 { return _M_node != __x._M_node; }
223 template<typename _Tp>
224 struct _Rb_tree_const_iterator
226 typedef _Tp value_type;
227 typedef const _Tp& reference;
228 typedef const _Tp* pointer;
230 typedef _Rb_tree_iterator<_Tp> iterator;
232 typedef bidirectional_iterator_tag iterator_category;
233 typedef ptrdiff_t difference_type;
235 typedef _Rb_tree_const_iterator<_Tp> _Self;
236 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
237 typedef const _Rb_tree_node<_Tp>* _Link_type;
239 _Rb_tree_const_iterator()
243 _Rb_tree_const_iterator(_Link_type __x)
246 _Rb_tree_const_iterator(const iterator& __it)
247 : _M_node(__it._M_node) { }
251 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
255 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
260 _M_node = _Rb_tree_increment(_M_node);
268 _M_node = _Rb_tree_increment(_M_node);
275 _M_node = _Rb_tree_decrement(_M_node);
283 _M_node = _Rb_tree_decrement(_M_node);
288 operator==(const _Self& __x) const
289 { return _M_node == __x._M_node; }
292 operator!=(const _Self& __x) const
293 { return _M_node != __x._M_node; }
298 template<typename _Val>
300 operator==(const _Rb_tree_iterator<_Val>& __x,
301 const _Rb_tree_const_iterator<_Val>& __y)
302 { return __x._M_node == __y._M_node; }
304 template<typename _Val>
306 operator!=(const _Rb_tree_iterator<_Val>& __x,
307 const _Rb_tree_const_iterator<_Val>& __y)
308 { return __x._M_node != __y._M_node; }
311 _Rb_tree_rotate_left(_Rb_tree_node_base* const __x,
312 _Rb_tree_node_base*& __root);
315 _Rb_tree_rotate_right(_Rb_tree_node_base* const __x,
316 _Rb_tree_node_base*& __root);
319 _Rb_tree_insert_and_rebalance(const bool __insert_left,
320 _Rb_tree_node_base* __x,
321 _Rb_tree_node_base* __p,
322 _Rb_tree_node_base& __header);
325 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
326 _Rb_tree_node_base& __header);
329 template<typename _Key, typename _Val, typename _KeyOfValue,
330 typename _Compare, typename _Alloc = allocator<_Val> >
333 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
337 typedef _Rb_tree_node_base* _Base_ptr;
338 typedef const _Rb_tree_node_base* _Const_Base_ptr;
339 typedef _Rb_tree_node<_Val> _Rb_tree_node;
342 typedef _Key key_type;
343 typedef _Val value_type;
344 typedef value_type* pointer;
345 typedef const value_type* const_pointer;
346 typedef value_type& reference;
347 typedef const value_type& const_reference;
348 typedef _Rb_tree_node* _Link_type;
349 typedef const _Rb_tree_node* _Const_Link_type;
350 typedef size_t size_type;
351 typedef ptrdiff_t difference_type;
352 typedef _Alloc allocator_type;
355 _M_get_Node_allocator()
356 { return *static_cast<_Node_allocator*>(&this->_M_impl); }
358 const _Node_allocator&
359 _M_get_Node_allocator() const
360 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
363 get_allocator() const
364 { return allocator_type(_M_get_Node_allocator()); }
369 { return _M_impl._Node_allocator::allocate(1); }
372 _M_put_node(_Rb_tree_node* __p)
373 { _M_impl._Node_allocator::deallocate(__p, 1); }
376 _M_create_node(const value_type& __x)
378 _Link_type __tmp = _M_get_node();
380 { get_allocator().construct(&__tmp->_M_value_field, __x); }
384 __throw_exception_again;
390 _M_clone_node(_Const_Link_type __x)
392 _Link_type __tmp = _M_create_node(__x->_M_value_field);
393 __tmp->_M_color = __x->_M_color;
400 _M_destroy_node(_Link_type __p)
402 get_allocator().destroy(&__p->_M_value_field);
407 template<typename _Key_compare,
408 bool _Is_pod_comparator = std::__is_pod<_Key_compare>::__value>
409 struct _Rb_tree_impl : public _Node_allocator
411 _Key_compare _M_key_compare;
412 _Rb_tree_node_base _M_header;
413 size_type _M_node_count; // Keeps track of size of tree.
416 : _Node_allocator(), _M_key_compare(), _M_header(),
420 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
421 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
429 this->_M_header._M_color = _S_red;
430 this->_M_header._M_parent = 0;
431 this->_M_header._M_left = &this->_M_header;
432 this->_M_header._M_right = &this->_M_header;
436 // Specialization for _Comparison types that are not capable of
437 // being base classes / super classes.
438 template<typename _Key_compare>
439 struct _Rb_tree_impl<_Key_compare, true> : public _Node_allocator
441 _Key_compare _M_key_compare;
442 _Rb_tree_node_base _M_header;
443 size_type _M_node_count; // Keeps track of size of tree.
446 : _Node_allocator(), _M_key_compare(), _M_header(),
450 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
451 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
459 this->_M_header._M_color = _S_red;
460 this->_M_header._M_parent = 0;
461 this->_M_header._M_left = &this->_M_header;
462 this->_M_header._M_right = &this->_M_header;
466 _Rb_tree_impl<_Compare> _M_impl;
471 { return this->_M_impl._M_header._M_parent; }
475 { return this->_M_impl._M_header._M_parent; }
479 { return this->_M_impl._M_header._M_left; }
483 { return this->_M_impl._M_header._M_left; }
487 { return this->_M_impl._M_header._M_right; }
491 { return this->_M_impl._M_header._M_right; }
495 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
500 return static_cast<_Const_Link_type>
501 (this->_M_impl._M_header._M_parent);
506 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
510 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
512 static const_reference
513 _S_value(_Const_Link_type __x)
514 { return __x->_M_value_field; }
517 _S_key(_Const_Link_type __x)
518 { return _KeyOfValue()(_S_value(__x)); }
521 _S_left(_Base_ptr __x)
522 { return static_cast<_Link_type>(__x->_M_left); }
524 static _Const_Link_type
525 _S_left(_Const_Base_ptr __x)
526 { return static_cast<_Const_Link_type>(__x->_M_left); }
529 _S_right(_Base_ptr __x)
530 { return static_cast<_Link_type>(__x->_M_right); }
532 static _Const_Link_type
533 _S_right(_Const_Base_ptr __x)
534 { return static_cast<_Const_Link_type>(__x->_M_right); }
536 static const_reference
537 _S_value(_Const_Base_ptr __x)
538 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
541 _S_key(_Const_Base_ptr __x)
542 { return _KeyOfValue()(_S_value(__x)); }
545 _S_minimum(_Base_ptr __x)
546 { return _Rb_tree_node_base::_S_minimum(__x); }
548 static _Const_Base_ptr
549 _S_minimum(_Const_Base_ptr __x)
550 { return _Rb_tree_node_base::_S_minimum(__x); }
553 _S_maximum(_Base_ptr __x)
554 { return _Rb_tree_node_base::_S_maximum(__x); }
556 static _Const_Base_ptr
557 _S_maximum(_Const_Base_ptr __x)
558 { return _Rb_tree_node_base::_S_maximum(__x); }
561 typedef _Rb_tree_iterator<value_type> iterator;
562 typedef _Rb_tree_const_iterator<value_type> const_iterator;
564 typedef std::reverse_iterator<iterator> reverse_iterator;
565 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
569 _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
571 // _GLIBCXX_RESOLVE_LIB_DEFECTS
572 // 233. Insertion hints in associative containers.
574 _M_insert_lower(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
577 _M_insert(_Const_Base_ptr __x, _Const_Base_ptr __y,
578 const value_type& __v);
581 _M_copy(_Const_Link_type __x, _Link_type __p);
584 _M_erase(_Link_type __x);
587 // allocation/deallocation
591 _Rb_tree(const _Compare& __comp,
592 const allocator_type& __a = allocator_type())
593 : _M_impl(__comp, __a)
596 _Rb_tree(const _Rb_tree& __x)
597 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
599 if (__x._M_root() != 0)
601 _M_root() = _M_copy(__x._M_begin(), _M_end());
602 _M_leftmost() = _S_minimum(_M_root());
603 _M_rightmost() = _S_maximum(_M_root());
604 _M_impl._M_node_count = __x._M_impl._M_node_count;
609 { _M_erase(_M_begin()); }
612 operator=(const _Rb_tree& __x);
617 { return _M_impl._M_key_compare; }
622 return iterator(static_cast<_Link_type>
623 (this->_M_impl._M_header._M_left));
629 return const_iterator(static_cast<_Const_Link_type>
630 (this->_M_impl._M_header._M_left));
635 { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
640 return const_iterator(static_cast<_Const_Link_type>
641 (&this->_M_impl._M_header));
646 { return reverse_iterator(end()); }
648 const_reverse_iterator
650 { return const_reverse_iterator(end()); }
654 { return reverse_iterator(begin()); }
656 const_reverse_iterator
658 { return const_reverse_iterator(begin()); }
662 { return _M_impl._M_node_count == 0; }
666 { return _M_impl._M_node_count; }
670 { return get_allocator().max_size(); }
677 _M_insert_unique(const value_type& __x);
680 _M_insert_equal(const value_type& __x);
682 // _GLIBCXX_RESOLVE_LIB_DEFECTS
683 // 233. Insertion hints in associative containers.
685 _M_insert_equal_lower(const value_type& __x);
688 _M_insert_unique(iterator __position, const value_type& __x);
691 _M_insert_unique(const_iterator __position, const value_type& __x);
694 _M_insert_equal(iterator __position, const value_type& __x);
697 _M_insert_equal(const_iterator __position, const value_type& __x);
699 template<typename _InputIterator>
701 _M_insert_unique(_InputIterator __first, _InputIterator __last);
703 template<typename _InputIterator>
705 _M_insert_equal(_InputIterator __first, _InputIterator __last);
708 erase(iterator __position);
711 erase(const_iterator __position);
714 erase(const key_type& __x);
717 erase(iterator __first, iterator __last);
720 erase(const_iterator __first, const_iterator __last);
723 erase(const key_type* __first, const key_type* __last);
728 _M_erase(_M_begin());
729 _M_leftmost() = _M_end();
731 _M_rightmost() = _M_end();
732 _M_impl._M_node_count = 0;
737 find(const key_type& __x);
740 find(const key_type& __x) const;
743 count(const key_type& __x) const;
746 lower_bound(const key_type& __x);
749 lower_bound(const key_type& __x) const;
752 upper_bound(const key_type& __x);
755 upper_bound(const key_type& __x) const;
757 pair<iterator,iterator>
758 equal_range(const key_type& __x);
760 pair<const_iterator, const_iterator>
761 equal_range(const key_type& __x) const;
768 template<typename _Key, typename _Val, typename _KeyOfValue,
769 typename _Compare, typename _Alloc>
771 operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
772 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
774 return __x.size() == __y.size()
775 && std::equal(__x.begin(), __x.end(), __y.begin());
778 template<typename _Key, typename _Val, typename _KeyOfValue,
779 typename _Compare, typename _Alloc>
781 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
782 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
784 return std::lexicographical_compare(__x.begin(), __x.end(),
785 __y.begin(), __y.end());
788 template<typename _Key, typename _Val, typename _KeyOfValue,
789 typename _Compare, typename _Alloc>
791 operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
792 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
793 { return !(__x == __y); }
795 template<typename _Key, typename _Val, typename _KeyOfValue,
796 typename _Compare, typename _Alloc>
798 operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
799 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
800 { return __y < __x; }
802 template<typename _Key, typename _Val, typename _KeyOfValue,
803 typename _Compare, typename _Alloc>
805 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
806 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
807 { return !(__y < __x); }
809 template<typename _Key, typename _Val, typename _KeyOfValue,
810 typename _Compare, typename _Alloc>
812 operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
813 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
814 { return !(__x < __y); }
816 template<typename _Key, typename _Val, typename _KeyOfValue,
817 typename _Compare, typename _Alloc>
819 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
820 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
823 template<typename _Key, typename _Val, typename _KeyOfValue,
824 typename _Compare, typename _Alloc>
825 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
826 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
827 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
831 // Note that _Key may be a constant type.
833 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
834 if (__x._M_root() != 0)
836 _M_root() = _M_copy(__x._M_begin(), _M_end());
837 _M_leftmost() = _S_minimum(_M_root());
838 _M_rightmost() = _S_maximum(_M_root());
839 _M_impl._M_node_count = __x._M_impl._M_node_count;
845 template<typename _Key, typename _Val, typename _KeyOfValue,
846 typename _Compare, typename _Alloc>
847 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
848 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
849 _M_insert(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
851 bool __insert_left = (__x != 0 || __p == _M_end()
852 || _M_impl._M_key_compare(_KeyOfValue()(__v),
855 _Link_type __z = _M_create_node(__v);
857 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
858 this->_M_impl._M_header);
859 ++_M_impl._M_node_count;
860 return iterator(__z);
863 template<typename _Key, typename _Val, typename _KeyOfValue,
864 typename _Compare, typename _Alloc>
865 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
866 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
867 _M_insert_lower(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
869 bool __insert_left = (__x != 0 || __p == _M_end()
870 || !_M_impl._M_key_compare(_S_key(__p),
871 _KeyOfValue()(__v)));
873 _Link_type __z = _M_create_node(__v);
875 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
876 this->_M_impl._M_header);
877 ++_M_impl._M_node_count;
878 return iterator(__z);
881 template<typename _Key, typename _Val, typename _KeyOfValue,
882 typename _Compare, typename _Alloc>
883 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
884 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
885 _M_insert(_Const_Base_ptr __x, _Const_Base_ptr __p, const _Val& __v)
887 bool __insert_left = (__x != 0 || __p == _M_end()
888 || _M_impl._M_key_compare(_KeyOfValue()(__v),
891 _Link_type __z = _M_create_node(__v);
893 _Rb_tree_insert_and_rebalance(__insert_left, __z,
894 const_cast<_Base_ptr>(__p),
895 this->_M_impl._M_header);
896 ++_M_impl._M_node_count;
897 return const_iterator(__z);
900 template<typename _Key, typename _Val, typename _KeyOfValue,
901 typename _Compare, typename _Alloc>
902 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
903 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
904 _M_insert_equal(const _Val& __v)
906 _Link_type __x = _M_begin();
907 _Link_type __y = _M_end();
911 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
912 _S_left(__x) : _S_right(__x);
914 return _M_insert(__x, __y, __v);
917 template<typename _Key, typename _Val, typename _KeyOfValue,
918 typename _Compare, typename _Alloc>
919 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
920 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
921 _M_insert_equal_lower(const _Val& __v)
923 _Link_type __x = _M_begin();
924 _Link_type __y = _M_end();
928 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
929 _S_left(__x) : _S_right(__x);
931 return _M_insert_lower(__x, __y, __v);
934 template<typename _Key, typename _Val, typename _KeyOfValue,
935 typename _Compare, typename _Alloc>
937 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
938 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
942 if (__t._M_root() != 0)
944 _M_root() = __t._M_root();
945 _M_leftmost() = __t._M_leftmost();
946 _M_rightmost() = __t._M_rightmost();
947 _M_root()->_M_parent = _M_end();
950 __t._M_leftmost() = __t._M_end();
951 __t._M_rightmost() = __t._M_end();
954 else if (__t._M_root() == 0)
956 __t._M_root() = _M_root();
957 __t._M_leftmost() = _M_leftmost();
958 __t._M_rightmost() = _M_rightmost();
959 __t._M_root()->_M_parent = __t._M_end();
962 _M_leftmost() = _M_end();
963 _M_rightmost() = _M_end();
967 std::swap(_M_root(),__t._M_root());
968 std::swap(_M_leftmost(),__t._M_leftmost());
969 std::swap(_M_rightmost(),__t._M_rightmost());
971 _M_root()->_M_parent = _M_end();
972 __t._M_root()->_M_parent = __t._M_end();
974 // No need to swap header's color as it does not change.
975 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
976 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
978 // _GLIBCXX_RESOLVE_LIB_DEFECTS
979 // 431. Swapping containers with unequal allocators.
980 std::__alloc_swap<_Node_allocator>::
981 _S_do_it(_M_get_Node_allocator(), __t._M_get_Node_allocator());
984 template<typename _Key, typename _Val, typename _KeyOfValue,
985 typename _Compare, typename _Alloc>
986 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
987 _Compare, _Alloc>::iterator, bool>
988 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
989 _M_insert_unique(const _Val& __v)
991 _Link_type __x = _M_begin();
992 _Link_type __y = _M_end();
997 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
998 __x = __comp ? _S_left(__x) : _S_right(__x);
1000 iterator __j = iterator(__y);
1004 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
1008 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
1009 return pair<iterator, bool>(_M_insert(__x, __y, __v), true);
1010 return pair<iterator, bool>(__j, false);
1013 template<typename _Key, typename _Val, typename _KeyOfValue,
1014 typename _Compare, typename _Alloc>
1015 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1016 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1017 _M_insert_unique(iterator __position, const _Val& __v)
1020 if (__position._M_node == _M_end())
1023 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
1024 _KeyOfValue()(__v)))
1025 return _M_insert(0, _M_rightmost(), __v);
1027 return _M_insert_unique(__v).first;
1029 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1030 _S_key(__position._M_node)))
1032 // First, try before...
1033 iterator __before = __position;
1034 if (__position._M_node == _M_leftmost()) // begin()
1035 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
1036 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node),
1037 _KeyOfValue()(__v)))
1039 if (_S_right(__before._M_node) == 0)
1040 return _M_insert(0, __before._M_node, __v);
1042 return _M_insert(__position._M_node,
1043 __position._M_node, __v);
1046 return _M_insert_unique(__v).first;
1048 else if (_M_impl._M_key_compare(_S_key(__position._M_node),
1049 _KeyOfValue()(__v)))
1051 // ... then try after.
1052 iterator __after = __position;
1053 if (__position._M_node == _M_rightmost())
1054 return _M_insert(0, _M_rightmost(), __v);
1055 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1056 _S_key((++__after)._M_node)))
1058 if (_S_right(__position._M_node) == 0)
1059 return _M_insert(0, __position._M_node, __v);
1061 return _M_insert(__after._M_node, __after._M_node, __v);
1064 return _M_insert_unique(__v).first;
1067 return __position; // Equivalent keys.
1070 template<typename _Key, typename _Val, typename _KeyOfValue,
1071 typename _Compare, typename _Alloc>
1072 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1073 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1074 _M_insert_unique(const_iterator __position, const _Val& __v)
1077 if (__position._M_node == _M_end())
1080 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
1081 _KeyOfValue()(__v)))
1082 return _M_insert(0, _M_rightmost(), __v);
1084 return const_iterator(_M_insert_unique(__v).first);
1086 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1087 _S_key(__position._M_node)))
1089 // First, try before...
1090 const_iterator __before = __position;
1091 if (__position._M_node == _M_leftmost()) // begin()
1092 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
1093 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node),
1094 _KeyOfValue()(__v)))
1096 if (_S_right(__before._M_node) == 0)
1097 return _M_insert(0, __before._M_node, __v);
1099 return _M_insert(__position._M_node,
1100 __position._M_node, __v);
1103 return const_iterator(_M_insert_unique(__v).first);
1105 else if (_M_impl._M_key_compare(_S_key(__position._M_node),
1106 _KeyOfValue()(__v)))
1108 // ... then try after.
1109 const_iterator __after = __position;
1110 if (__position._M_node == _M_rightmost())
1111 return _M_insert(0, _M_rightmost(), __v);
1112 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1113 _S_key((++__after)._M_node)))
1115 if (_S_right(__position._M_node) == 0)
1116 return _M_insert(0, __position._M_node, __v);
1118 return _M_insert(__after._M_node, __after._M_node, __v);
1121 return const_iterator(_M_insert_unique(__v).first);
1124 return __position; // Equivalent keys.
1127 template<typename _Key, typename _Val, typename _KeyOfValue,
1128 typename _Compare, typename _Alloc>
1129 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1130 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1131 _M_insert_equal(iterator __position, const _Val& __v)
1134 if (__position._M_node == _M_end())
1137 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
1138 _S_key(_M_rightmost())))
1139 return _M_insert(0, _M_rightmost(), __v);
1141 return _M_insert_equal(__v);
1143 else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
1144 _KeyOfValue()(__v)))
1146 // First, try before...
1147 iterator __before = __position;
1148 if (__position._M_node == _M_leftmost()) // begin()
1149 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
1150 else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
1151 _S_key((--__before)._M_node)))
1153 if (_S_right(__before._M_node) == 0)
1154 return _M_insert(0, __before._M_node, __v);
1156 return _M_insert(__position._M_node,
1157 __position._M_node, __v);
1160 return _M_insert_equal(__v);
1164 // ... then try after.
1165 iterator __after = __position;
1166 if (__position._M_node == _M_rightmost())
1167 return _M_insert(0, _M_rightmost(), __v);
1168 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
1169 _KeyOfValue()(__v)))
1171 if (_S_right(__position._M_node) == 0)
1172 return _M_insert(0, __position._M_node, __v);
1174 return _M_insert(__after._M_node, __after._M_node, __v);
1177 return _M_insert_equal_lower(__v);
1181 template<typename _Key, typename _Val, typename _KeyOfValue,
1182 typename _Compare, typename _Alloc>
1183 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1184 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1185 _M_insert_equal(const_iterator __position, const _Val& __v)
1188 if (__position._M_node == _M_end())
1191 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
1192 _S_key(_M_rightmost())))
1193 return _M_insert(0, _M_rightmost(), __v);
1195 return const_iterator(_M_insert_equal(__v));
1197 else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
1198 _KeyOfValue()(__v)))
1200 // First, try before...
1201 const_iterator __before = __position;
1202 if (__position._M_node == _M_leftmost()) // begin()
1203 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
1204 else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
1205 _S_key((--__before)._M_node)))
1207 if (_S_right(__before._M_node) == 0)
1208 return _M_insert(0, __before._M_node, __v);
1210 return _M_insert(__position._M_node,
1211 __position._M_node, __v);
1214 return const_iterator(_M_insert_equal(__v));
1218 // ... then try after.
1219 const_iterator __after = __position;
1220 if (__position._M_node == _M_rightmost())
1221 return _M_insert(0, _M_rightmost(), __v);
1222 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
1223 _KeyOfValue()(__v)))
1225 if (_S_right(__position._M_node) == 0)
1226 return _M_insert(0, __position._M_node, __v);
1228 return _M_insert(__after._M_node, __after._M_node, __v);
1231 return const_iterator(_M_insert_equal_lower(__v));
1235 template<typename _Key, typename _Val, typename _KoV,
1236 typename _Cmp, typename _Alloc>
1239 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1240 _M_insert_equal(_II __first, _II __last)
1242 for (; __first != __last; ++__first)
1243 _M_insert_equal(end(), *__first);
1246 template<typename _Key, typename _Val, typename _KoV,
1247 typename _Cmp, typename _Alloc>
1250 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1251 _M_insert_unique(_II __first, _II __last)
1253 for (; __first != __last; ++__first)
1254 _M_insert_unique(end(), *__first);
1257 template<typename _Key, typename _Val, typename _KeyOfValue,
1258 typename _Compare, typename _Alloc>
1260 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1261 erase(iterator __position)
1264 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1265 (__position._M_node,
1266 this->_M_impl._M_header));
1267 _M_destroy_node(__y);
1268 --_M_impl._M_node_count;
1271 template<typename _Key, typename _Val, typename _KeyOfValue,
1272 typename _Compare, typename _Alloc>
1274 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1275 erase(const_iterator __position)
1278 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1279 (const_cast<_Base_ptr>(__position._M_node),
1280 this->_M_impl._M_header));
1281 _M_destroy_node(__y);
1282 --_M_impl._M_node_count;
1285 template<typename _Key, typename _Val, typename _KeyOfValue,
1286 typename _Compare, typename _Alloc>
1287 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1288 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1289 erase(const _Key& __x)
1291 pair<iterator, iterator> __p = equal_range(__x);
1292 const size_type __old_size = size();
1293 erase(__p.first, __p.second);
1294 return __old_size - size();
1297 template<typename _Key, typename _Val, typename _KoV,
1298 typename _Compare, typename _Alloc>
1299 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1300 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1301 _M_copy(_Const_Link_type __x, _Link_type __p)
1303 // Structural copy. __x and __p must be non-null.
1304 _Link_type __top = _M_clone_node(__x);
1305 __top->_M_parent = __p;
1310 __top->_M_right = _M_copy(_S_right(__x), __top);
1316 _Link_type __y = _M_clone_node(__x);
1318 __y->_M_parent = __p;
1320 __y->_M_right = _M_copy(_S_right(__x), __y);
1328 __throw_exception_again;
1333 template<typename _Key, typename _Val, typename _KeyOfValue,
1334 typename _Compare, typename _Alloc>
1336 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1337 _M_erase(_Link_type __x)
1339 // Erase without rebalancing.
1342 _M_erase(_S_right(__x));
1343 _Link_type __y = _S_left(__x);
1344 _M_destroy_node(__x);
1349 template<typename _Key, typename _Val, typename _KeyOfValue,
1350 typename _Compare, typename _Alloc>
1352 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1353 erase(iterator __first, iterator __last)
1355 if (__first == begin() && __last == end())
1358 while (__first != __last)
1362 template<typename _Key, typename _Val, typename _KeyOfValue,
1363 typename _Compare, typename _Alloc>
1365 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1366 erase(const_iterator __first, const_iterator __last)
1368 if (__first == begin() && __last == end())
1371 while (__first != __last)
1375 template<typename _Key, typename _Val, typename _KeyOfValue,
1376 typename _Compare, typename _Alloc>
1378 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1379 erase(const _Key* __first, const _Key* __last)
1381 while (__first != __last)
1385 template<typename _Key, typename _Val, typename _KeyOfValue,
1386 typename _Compare, typename _Alloc>
1387 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1388 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1389 find(const _Key& __k)
1391 _Link_type __x = _M_begin(); // Current node.
1392 _Link_type __y = _M_end(); // Last node which is not less than __k.
1395 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1396 __y = __x, __x = _S_left(__x);
1398 __x = _S_right(__x);
1400 iterator __j = iterator(__y);
1401 return (__j == end()
1402 || _M_impl._M_key_compare(__k,
1403 _S_key(__j._M_node))) ? end() : __j;
1406 template<typename _Key, typename _Val, typename _KeyOfValue,
1407 typename _Compare, typename _Alloc>
1408 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1409 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1410 find(const _Key& __k) const
1412 _Const_Link_type __x = _M_begin(); // Current node.
1413 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1417 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1418 __y = __x, __x = _S_left(__x);
1420 __x = _S_right(__x);
1422 const_iterator __j = const_iterator(__y);
1423 return (__j == end()
1424 || _M_impl._M_key_compare(__k,
1425 _S_key(__j._M_node))) ? end() : __j;
1428 template<typename _Key, typename _Val, typename _KeyOfValue,
1429 typename _Compare, typename _Alloc>
1430 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1431 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1432 count(const _Key& __k) const
1434 pair<const_iterator, const_iterator> __p = equal_range(__k);
1435 const size_type __n = std::distance(__p.first, __p.second);
1439 template<typename _Key, typename _Val, typename _KeyOfValue,
1440 typename _Compare, typename _Alloc>
1441 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1442 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1443 lower_bound(const _Key& __k)
1445 _Link_type __x = _M_begin(); // Current node.
1446 _Link_type __y = _M_end(); // Last node which is not less than __k.
1449 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1450 __y = __x, __x = _S_left(__x);
1452 __x = _S_right(__x);
1454 return iterator(__y);
1457 template<typename _Key, typename _Val, typename _KeyOfValue,
1458 typename _Compare, typename _Alloc>
1459 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1460 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1461 lower_bound(const _Key& __k) const
1463 _Const_Link_type __x = _M_begin(); // Current node.
1464 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1467 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1468 __y = __x, __x = _S_left(__x);
1470 __x = _S_right(__x);
1472 return const_iterator(__y);
1475 template<typename _Key, typename _Val, typename _KeyOfValue,
1476 typename _Compare, typename _Alloc>
1477 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1478 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1479 upper_bound(const _Key& __k)
1481 _Link_type __x = _M_begin(); // Current node.
1482 _Link_type __y = _M_end(); // Last node which is greater than __k.
1485 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1486 __y = __x, __x = _S_left(__x);
1488 __x = _S_right(__x);
1490 return iterator(__y);
1493 template<typename _Key, typename _Val, typename _KeyOfValue,
1494 typename _Compare, typename _Alloc>
1495 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1496 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1497 upper_bound(const _Key& __k) const
1499 _Const_Link_type __x = _M_begin(); // Current node.
1500 _Const_Link_type __y = _M_end(); // Last node which is greater than __k.
1503 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1504 __y = __x, __x = _S_left(__x);
1506 __x = _S_right(__x);
1508 return const_iterator(__y);
1511 template<typename _Key, typename _Val, typename _KeyOfValue,
1512 typename _Compare, typename _Alloc>
1514 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1515 _Compare, _Alloc>::iterator,
1516 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator>
1517 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1518 equal_range(const _Key& __k)
1519 { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
1521 template<typename _Key, typename _Val, typename _KoV,
1522 typename _Compare, typename _Alloc>
1524 pair<typename _Rb_tree<_Key, _Val, _KoV,
1525 _Compare, _Alloc>::const_iterator,
1526 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
1527 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1528 equal_range(const _Key& __k) const
1529 { return pair<const_iterator, const_iterator>(lower_bound(__k),
1530 upper_bound(__k)); }
1533 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1534 const _Rb_tree_node_base* __root);
1536 template<typename _Key, typename _Val, typename _KeyOfValue,
1537 typename _Compare, typename _Alloc>
1539 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1541 if (_M_impl._M_node_count == 0 || begin() == end())
1542 return _M_impl._M_node_count == 0 && begin() == end()
1543 && this->_M_impl._M_header._M_left == _M_end()
1544 && this->_M_impl._M_header._M_right == _M_end();
1546 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1547 for (const_iterator __it = begin(); __it != end(); ++__it)
1549 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1550 _Const_Link_type __L = _S_left(__x);
1551 _Const_Link_type __R = _S_right(__x);
1553 if (__x->_M_color == _S_red)
1554 if ((__L && __L->_M_color == _S_red)
1555 || (__R && __R->_M_color == _S_red))
1558 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1560 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1563 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1567 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1569 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1574 _GLIBCXX_END_NAMESPACE